Swift使用LAContext实现指纹解锁

通过LAContext的使用,调用系统指纹解锁功能来实现APP的解锁。

import LocalAuthentication

class ViewController: UIViewController{
   
    var isAvailable:Bool?
    
    @IBOutlet weak var statusLab: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        laContext = LAContext.init()
//        //判断touchid在应用中是否可用
//       var isCanEvaluatePolicy = laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: <#T##NSErrorPointer#>)
    }
   
    @IBAction func checkTouchBtnClick(_ sender: Any) {
       let context = LAContext()
       var error:NSError?
       let isTouchIdAvailable = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
        if isTouchIdAvailable == false{
            statusLab.text = "touch id不能用"
            isAvailable = false
        }else{
            statusLab.text = "touch id能用"
            isAvailable = true;
        }
    }
    @IBAction func authTouchBtnClick(_ sender: Any) {
        if isAvailable != nil && isAvailable == true{
            
            let context = LAContext()
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "请求使用touch id") {[weak self] (bool, error) in
                if bool{
                    self?.statusLab.text = "通过验证"
                }else{
                    self?.statusLab.text = "没通过验证"
                }
            }
            
        }else{
            print("-----------touch id不能用")
        }
    }
}
posted @ 2019-12-13 11:26  新年新气象  阅读(654)  评论(0)    收藏  举报