我的应用程序支持打开其他应用程序中的文档,例如图像,pdf。 Tocuh 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 {
}
})
现在,当用户从后台打开应用或重新启动应用程序时,请求触摸ID的工作正常。 当从其他应用程序打开该应用程序时会出现问题,例如点击应用程序URL,使用“复制到MyApp”选项从外部应用程序共享文档,其中,AppDelegate的打开url方法的调用如下所示
public func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
//validate and save url
return true
}
问题是从外部应用程序启动应用程序时,将调用上述打开的url方法,并且还会按预期调用 UIApplicationWillEnterForeground 观察器。 但是在该UIApplicationWillEnterForeground观察器中,LAContext()。evaluatePolicy突然失败,并显示错误消息“呼叫者移至后台”。
请注意,该问题可以在iOS 11.0.3、11.3 上看到,而在iOS 11.4或<11
上无法再现答案 0 :(得分:1)
您需要在应用为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")
}
}
})
}
})