我的应用程序支持从其他应用程序打开图像,pdf等文档。触摸ID实现如下所示,当应用程序到达前台时请求它
NotificationCenter.default.addObserver(forName: .UIApplicationWillEnterForeground, object: nil, queue: .main) { (notification) in
LAContext().evaluatePolicy( .deviceOwnerAuthenticationWithBiometrics, localizedReason: "Request Touch ID", reply: { [unowned self] (success, error) -> Void in
if (success) {
} else {
}
})
现在,当用户从后台或重新启动时打开应用程序时,请求Touch Id正常工作。从其他应用程序打开应用程序时会出现问题,例如点击应用程序URL,使用“复制到MyApp”选项从外部应用程序共享文档,其中AppDelegate的打开URL方法被调用,如下所示
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
//validate and save url
return true
}
问题是当从外部应用程序启动应用程序时,会调用上面的open url方法,并且还会按预期调用UIApplicationWillEnterForeground观察器。但是在那个UIApplicationWillEnterForeground观察者中,LAContext()。evaluatePolicy突然失败并显示“调用者移动到后台”错误。
注意,问题可以在iOS 11.0.3,11.3上看到,而iOS 11.4或<11则无法重现
当app是applicationDidBecomeActive
时你需要添加它
NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive, object: nil, queue: .main) { (notification) in
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(
LAPolicy.deviceOwnerAuthenticationWithBiometrics,
error: &error) {
// Device can use biometric authentication
context.evaluatePolicy(
LAPolicy.deviceOwnerAuthenticationWithBiometrics,
localizedReason: "Access requires authentication",
reply: {(success, error) in
DispatchQueue.main.async {
if let err = error {
switch err._code {
case LAError.Code.systemCancel.rawValue:
self.notifyUser("Session cancelled",
err: err.localizedDescription)
case LAError.Code.userCancel.rawValue:
self.notifyUser("Please try again",
err: err.localizedDescription)
case LAError.Code.userFallback.rawValue:
self.notifyUser("Authentication",
err: "Password option selected")
// Custom code to obtain password here
default:
self.notifyUser("Authentication failed",
err: err.localizedDescription)
}
} else {
self.notifyUser("Authentication Successful",
err: "You now have full access")
}
}
})
}
})