Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 5 #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 54 additions & 40 deletions PasscodeLock.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions PasscodeLock/Functions.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

import Foundation

func localizedStringFor(key: String, comment: String) -> String {
func localizedStringFor(_ key: String, comment: String) -> String {

let name = "PasscodeLock"
let bundle = bundleForResource(name, ofType: "strings")

return NSLocalizedString(key, tableName: name, bundle: bundle, comment: comment)
}

func bundleForResource(name: String, ofType type: String) -> NSBundle {
func bundleForResource(_ name: String, ofType type: String) -> Bundle {

if(NSBundle.mainBundle().pathForResource(name, ofType: type) != nil) {
return NSBundle.mainBundle()
if(Bundle.main.path(forResource: name, ofType: type) != nil) {
return Bundle.main
}

return NSBundle(forClass: PasscodeLock.self)
return Bundle(for: PasscodeLock.self)
}
Empty file modified PasscodeLock/Info.plist
100644 → 100755
Empty file.
Empty file modified PasscodeLock/PasscodeLock.h
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion PasscodeLock/PasscodeLock/ChangePasscodeState.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct ChangePasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockChangeDescription", comment: "Change passcode description")
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

guard let currentPasscode = lock.repository.passcode else {
return
Expand Down
4 changes: 2 additions & 2 deletions PasscodeLock/PasscodeLock/ConfirmPasscodeState.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ConfirmPasscodeState: PasscodeLockStateType {
let isCancellableAction = true
var isTouchIDAllowed = false

private var passcodeToConfirm: [String]
fileprivate var passcodeToConfirm: [String]

init(passcode: [String]) {

Expand All @@ -24,7 +24,7 @@ struct ConfirmPasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockConfirmDescription", comment: "Confirm passcode description")
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

if passcode == passcodeToConfirm {

Expand Down
29 changes: 21 additions & 8 deletions PasscodeLock/PasscodeLock/EnterPasscodeState.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ struct EnterPasscodeState: PasscodeLockStateType {
let isCancellableAction: Bool
var isTouchIDAllowed = true

private var inccorectPasscodeAttempts = 0
static let incorrectPasscodeAttemptsKey = "incorrectPasscodeAttempts"
static var incorrectPasscodeAttempts: Int {
get {
return UserDefaults.standard.integer(forKey: incorrectPasscodeAttemptsKey)
}
set {
UserDefaults.standard.set(newValue, forKey: incorrectPasscodeAttemptsKey)
}
}

private var isNotificationSent = false

init(allowCancellation: Bool = false) {
Expand All @@ -27,36 +36,40 @@ struct EnterPasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockEnterDescription", comment: "Enter passcode description")
}

mutating func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
mutating func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

guard let currentPasscode = lock.repository.passcode else {
return
}

var incorrectPasscodeAttempts = EnterPasscodeState.incorrectPasscodeAttempts
if passcode == currentPasscode {

lock.delegate?.passcodeLockDidSucceed(lock)

incorrectPasscodeAttempts = 0
} else {

inccorectPasscodeAttempts += 1
incorrectPasscodeAttempts += 1

if inccorectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {
if incorrectPasscodeAttempts >= lock.configuration.maximumInccorectPasscodeAttempts {

postNotification()
incorrectPasscodeAttempts = 0
}

lock.delegate?.passcodeLockDidFail(lock)
}

EnterPasscodeState.incorrectPasscodeAttempts = incorrectPasscodeAttempts
}

private mutating func postNotification() {
fileprivate mutating func postNotification() {

guard !isNotificationSent else { return }

let center = NSNotificationCenter.defaultCenter()
let center = NotificationCenter.default

center.postNotificationName(PasscodeLockIncorrectPasscodeNotification, object: nil)
center.post(name: Notification.Name(rawValue: PasscodeLockIncorrectPasscodeNotification), object: nil)

isNotificationSent = true
}
Expand Down
47 changes: 27 additions & 20 deletions PasscodeLock/PasscodeLock/PasscodeLock.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
import Foundation
import LocalAuthentication

public class PasscodeLock: PasscodeLockType {
open class PasscodeLock: PasscodeLockType {

public weak var delegate: PasscodeLockTypeDelegate?
open weak var delegate: PasscodeLockTypeDelegate?
public let configuration: PasscodeLockConfigurationType

public var repository: PasscodeRepositoryType {
open var repository: PasscodeRepositoryType {
return configuration.repository
}

public var state: PasscodeLockStateType {
open var state: PasscodeLockStateType {
return lockState
}

public var isTouchIDAllowed: Bool {
open var isTouchIDAllowed: Bool {
return isTouchIDEnabled() && configuration.isTouchIDAllowed && lockState.isTouchIDAllowed
}

private var lockState: PasscodeLockStateType
private lazy var passcode = [String]()
fileprivate var lockState: PasscodeLockStateType
fileprivate lazy var passcode = [String]()

public init(state: PasscodeLockStateType, configuration: PasscodeLockConfigurationType) {

Expand All @@ -37,63 +37,70 @@ public class PasscodeLock: PasscodeLockType {
self.configuration = configuration
}

public func addSign(sign: String) {
open func addSign(_ sign: String) {

passcode.append(sign)
delegate?.passcodeLock(self, addedSignAtIndex: passcode.count - 1)

if passcode.count >= configuration.passcodeLength {

lockState.acceptPasscode(passcode, fromLock: self)
passcode.removeAll(keepCapacity: true)
// handles "requires exclusive access" error at Swift 4
var lockStateCopy = lockState
lockStateCopy.acceptPasscode(passcode, fromLock: self)
passcode.removeAll(keepingCapacity: true)
}
}

public func removeSign() {
open func removeSign() {

guard passcode.count > 0 else { return }

passcode.removeLast()
delegate?.passcodeLock(self, removedSignAtIndex: passcode.count)
}

public func changeStateTo(state: PasscodeLockStateType) {
open func changeStateTo(_ state: PasscodeLockStateType) {

lockState = state
delegate?.passcodeLockDidChangeState(self)
}

public func authenticateWithBiometrics() {
open func authenticateWithBiometrics() {

guard isTouchIDAllowed else { return }

let context = LAContext()
let reason = localizedStringFor("PasscodeLockTouchIDReason", comment: "TouchID authentication reason")
let reason: String
if let configReason = configuration.touchIdReason {
reason = configReason
} else {
reason = localizedStringFor("PasscodeLockTouchIDReason", comment: "TouchID authentication reason")
}

context.localizedFallbackTitle = localizedStringFor("PasscodeLockTouchIDButton", comment: "TouchID authentication fallback button")

context.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
success, error in

self.handleTouchIDResult(success)
}
}

private func handleTouchIDResult(success: Bool) {
fileprivate func handleTouchIDResult(_ success: Bool) {

dispatch_async(dispatch_get_main_queue()) {
DispatchQueue.main.async {

if success {

EnterPasscodeState.incorrectPasscodeAttempts = 0
self.delegate?.passcodeLockDidSucceed(self)
}
}
}

private func isTouchIDEnabled() -> Bool {
fileprivate func isTouchIDEnabled() -> Bool {

let context = LAContext()

return context.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, error: nil)
return context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
}
}
2 changes: 1 addition & 1 deletion PasscodeLock/PasscodeLock/SetPasscodeState.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct SetPasscodeState: PasscodeLockStateType {
description = localizedStringFor("PasscodeLockSetDescription", comment: "Set passcode description")
}

func acceptPasscode(passcode: [String], fromLock lock: PasscodeLockType) {
func acceptPasscode(_ passcode: [String], fromLock lock: PasscodeLockType) {

let nextState = ConfirmPasscodeState(passcode: passcode)

Expand Down
96 changes: 51 additions & 45 deletions PasscodeLock/PasscodeLockPresenter.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,66 @@

import UIKit

public class PasscodeLockPresenter {
open class PasscodeLockPresenter {

private var mainWindow: UIWindow?
fileprivate var mainWindow: UIWindow?

private lazy var passcodeLockWindow: UIWindow = {
fileprivate lazy var passcodeLockWindow: UIWindow = {

let window = UIWindow(frame: UIScreen.mainScreen().bounds)
let window = UIWindow(frame: UIScreen.main.bounds)

window.windowLevel = 0
window.windowLevel = UIWindow.Level(rawValue: 0)
window.makeKeyAndVisible()

return window
}()

private let passcodeConfiguration: PasscodeLockConfigurationType
public var isPasscodePresented = false
fileprivate let passcodeConfiguration: PasscodeLockConfigurationType
open var isPasscodePresented = false

public let passcodeLockVC: PasscodeLockViewController

public init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType, viewController: PasscodeLockViewController) {

mainWindow = window
mainWindow?.windowLevel = 1
mainWindow?.windowLevel = UIWindow.Level(rawValue: 1)
passcodeConfiguration = configuration

passcodeLockVC = viewController
}

public convenience init(mainWindow window: UIWindow?, configuration: PasscodeLockConfigurationType) {

let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: configuration)
let passcodeLockVC = PasscodeLockViewController(state: .enterPasscode, configuration: configuration)

self.init(mainWindow: window, configuration: configuration, viewController: passcodeLockVC)
}

public func presentPasscodeLock() {
// HACK: below function that handles not presenting the keyboard in case Passcode is presented
// is a smell in the code that had to be introduced for iOS9 where Apple decided to move the keyboard
// in a UIRemoteKeyboardWindow.
// This doesn't allow our Passcode Lock window to move on top of keyboard.
// Setting a higher windowLevel to our window or even trying to change keyboards'
// windowLevel has been tried without luck.
//
// Revise in a later version and remove the hack if not needed
func toggleKeyboardVisibility(hide: Bool) {
if let keyboardWindow = UIApplication.shared.windows.last,
keyboardWindow.description.hasPrefix("<UIRemoteKeyboardWindow")
{
keyboardWindow.alpha = hide ? 0.0 : 1.0
}
}

open func presentPasscodeLock() {

guard passcodeConfiguration.repository.hasPasscode else { return }
guard !isPasscodePresented else { return }

isPasscodePresented = true
passcodeLockWindow.windowLevel = UIWindow.Level(rawValue: 2)

passcodeLockWindow.windowLevel = 2
passcodeLockWindow.hidden = false

mainWindow?.windowLevel = 1
mainWindow?.endEditing(true)
toggleKeyboardVisibility(hide: true)

let passcodeLockVC = PasscodeLockViewController(state: .EnterPasscode, configuration: passcodeConfiguration)
let userDismissCompletionCallback = passcodeLockVC.dismissCompletionCallback

passcodeLockVC.dismissCompletionCallback = { [weak self] in
Expand All @@ -68,41 +80,35 @@ public class PasscodeLockPresenter {
passcodeLockWindow.rootViewController = passcodeLockVC
}

public func dismissPasscodeLock(animated animated: Bool = true) {
open func dismissPasscodeLock(animated: Bool = true) {

isPasscodePresented = false
mainWindow?.windowLevel = 1
mainWindow?.windowLevel = UIWindow.Level(rawValue: 1)
mainWindow?.makeKeyAndVisible()

if animated {

animatePasscodeLockDismissal()

UIView.animate(
withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [.curveEaseInOut],
animations: { [weak self] in

self?.passcodeLockWindow.alpha = 0
},
completion: { [weak self] _ in

self?.passcodeLockWindow.windowLevel = UIWindow.Level(rawValue: 0)
self?.passcodeLockWindow.rootViewController = nil
self?.passcodeLockWindow.alpha = 1
self?.toggleKeyboardVisibility(hide: false)
}
)
} else {

passcodeLockWindow.windowLevel = 0
passcodeLockWindow.windowLevel = UIWindow.Level(rawValue: 0)
passcodeLockWindow.rootViewController = nil
toggleKeyboardVisibility(hide: false)
}
}

internal func animatePasscodeLockDismissal() {

UIView.animateWithDuration(
0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [.CurveEaseInOut],
animations: { [weak self] in

self?.passcodeLockWindow.alpha = 0
},
completion: { [weak self] _ in

self?.passcodeLockWindow.windowLevel = 0
self?.passcodeLockWindow.rootViewController = nil
self?.passcodeLockWindow.alpha = 1
}
)
}
}
Loading