【问题标题】:iOS : Touch Id is not shown when AppDelegate's open url is invokediOS:调用 AppDelegate 的打开 url 时不显示 Touch Id
【发布时间】:2018-09-18 05:13:01
【问题描述】:

我的应用程序支持从其他应用程序打开图像、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 或 上无法重现

【问题讨论】:

    标签: ios swift touch-id openurl lacontext


    【解决方案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")
                    }
                }
        })
    
    }
    
    })
    

    【讨论】:

    • 试过UIApplicationDidBecomeActive,问题是,TouchId请求对话框关闭后再次调用观察者。
    • 问题不在addObserver 问题在LAContext().
    • @Anbu.karthik:我可以知道 LAContext() 有什么问题吗
    • @RajeshRs- 一旦你的应用进入非活动状态,试试这个 LAContext().invalidate() 使你的触摸 ID 无效
    • @Anbu.karthik:当 LAContext().evaluatePolicy( 被调用时,应用程序进入 UIApplicationWillResignActive 状态,如果我执行 lacontext.invalidate() 它会取消当前的 touchId 请求,因为它调用了 LAError.SystemCancel
    猜你喜欢
    • 2015-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2023-03-25
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    相关资源
    最近更新 更多