我目前正在使用LocalAuthentication
框架来实现TouchID。
我最初尝试提供服务的过程如下:
import Foundation
import LocalAuthentication
public enum AuthenticationState {
case unknown
case authenticated
case unauthenticated
public func isAuthenticated() -> Bool {
return self == .authenticated
}
}
public protocol TouchIDServiceType {
var authState: AuthenticationState { get }
func authenticate(reason: String, completion: @escaping (AuthenticationState) -> Void) -> Void
func removeAuthentication() -> Void
}
public protocol LAContextType: class {
func canEvaluatePolicy(_ policy: LAPolicy, error: NSErrorPointer) -> Bool
func evaluatePolicy(_ policy: LAPolicy, localizedReason: String, reply: @escaping (Bool, Error?) -> Void)
}
public class TouchIDService: TouchIDServiceType {
public var authState: AuthenticationState = .unknown
private var context: LAContextType
private var policy = LAPolicy.deviceOwnerAuthentication
public init(context: LAContextType = LAContext()) {
self.context = context
}
public func authenticate(reason: String, completion: @escaping (AuthenticationState) -> Void) -> Void {
var error: NSError?
if context.canEvaluatePolicy(policy, error: &error) {
context.evaluatePolicy(policy, localizedReason: reason) { (success, error) in
DispatchQueue.main.async {
if success {
self.authState = .authenticated
completion(.authenticated)
} else {
self.authState = .unauthenticated
completion(.unauthenticated)
}
}
}
} else {
authState = .authenticated
completion(.authenticated)
}
}
public func removeAuthentication() -> Void {
authState = .unknown
context = LAContext() // reset the context
}
}
extension LAContext: LAContextType { }
在我的AppDelegate
中,我有一个简单的函数返回一个布尔值,我用它来决定是否对用户进行身份验证是否要采取什么措施。
private func authenticate(completion: @escaping (Bool) -> Void) {
localAuthenticator.authenticate(reason: "Authentication is required") { state in
completion(state == .authenticated)
}
}
类似的东西:
@discardableResult
private func performLocalAuth() -> Bool {
if IdentityProvider.shared.alreadyLoggedIn() {
authenticate { [weak self] success in
if !success { IdentityProvider.shared.logout() }
}
return true
} else {
return false
}
}
我有一个要求,如果用户通过单击主页按钮取消质询,则不应将其视为失败的尝试,而应该在主页返回到前台时再次使用TouchID身份验证器进行质询。
仅当用户单击或取消或未通过挑战时,我才可以正确注销它们。
在这种情况下,用户不小心打开了应用程序,而是注销了应用程序,让他们简单地将其关闭。
我不确定应该如何以及在何处检查“主页”按钮是否被点击,甚至是可能的。