我的应用程序支持打开来自其他应用程序的图像、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 {
}
})
现在,当用户从后台打开应用程序或重新启动时,请求 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 突然失败并出现错误“Caller moved to background”。
请注意,问题可以在 iOS 11.0.3、11.3 上看到,而在 iOS 11.4 或 <11
上无法重现最佳答案
你需要在应用程序为 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")
}
}
})
}
})
关于iOS : Touch Id is not shown when AppDelegate's open url is invoked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52379360/