如何在iOS 10中使用TouchID

时间:2017-03-03 12:10:06

标签: ios swift3 touch-id

我想在我的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)
        }
    }
}

错误:

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:5)

没有类型转换的代码应该可以正常工作

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")
            }
        }
        )
    }
}