1

我的应用程序支持从其他应用程序打开图像、pdf 等文档。Tocuh Id 实现如下图,app进入前台时请求

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
}

问题是当应用程序从外部应用程序启动时,上面的打开 url 方法被调用,并且UIApplicationWillEnterForeground观察者也被按预期调用。但在那个 UIApplicationWillEnterForeground 观察者中,LAContext().evaluatePolicy 突然失败,并出现错误“调用者移至后台”。

请注意,问题可以在 iOS 11.0.3、11.3上看到,而在 iOS 11.4 或 <11 上无法重现

4

1 回答 1

2

您需要在应用程序运行时添加它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")
                }
            }
    })

}

})
于 2018-09-18T05:15:58.750 回答