【问题标题】:How to use TouchID in iOS 10如何在 iOS 10 中使用 TouchID
【发布时间】:2017-07-23 13:04:19
【问题描述】:

我想在我的 iOS 应用中实现本地身份验证安全性,但我 收到错误,但无法弄清楚为什么会出现此错误。

我正在使用 iPhone 5s。这有关系吗?

代码:

import UIKit
import LocalAuthentication

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func action(_ sender: Any) {
        authenticateUser()
    }

    func authenticateUser() {
        let authContext : LAContext = LAContext()
        var error: NSError?

        if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){
            authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {(successful: Bool, error: NSError?) -> Void in
                if successful{
                    print("TouchID Yes")
                }
                else{
                    print("TouchID No")
                }
                } as! (Bool, Error?) -> Void)
        }
        else{
            authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: {
                (successful: Bool, error: NSError?) in
                if successful{
                    print("PassCode Yes")
                }
                else{
                    print("PassCode No")
                }
                } as! (Bool, Error?) -> Void)
        }
    }
}

错误:

提前致谢。

【问题讨论】:

    标签: ios swift3 touch-id


    【解决方案1】:

    这段没有类型转换的代码应该可以工作

    func authenticateUser() {
        let authContext : LAContext = LAContext()
        var error: NSError?
    
        if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error){
            authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Biometric Check for application", reply: {successful, error -> Void in
                if successful{
                    print("TouchID Yes")
                }
                else{
                    print("TouchID No")
                }
            }
            )
        }
        else{
            authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: "Enter your Passcode", reply: {
                successful,error in
                if successful{
                    print("PassCode Yes")
                }
                else{
                    print("PassCode No")
                }
            }
            )
        }
    }
    

    【讨论】:

    • 请解释为什么这可以解决问题:这是因为您不能键入强制类型转换闭包,因为那没有意义。
    • 这不是将 NSError 强制转换为 Error,因为这不是你对强制转换所做的。闭包是一种类型,强制向下转换为不相关的类型总是会失败。 12 as! String 会抛出异常。同样,当您强制将((Bool, NSError?) -> Void) 向下转换为不相关的类型((Bool,Error?) -> Void) 时,您会得到一个异常。仅仅因为 NSError 和 Error 相关并不会使具有不同签名的闭包相关。
    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多