FaceID调用的几个注意点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/axman/article/details/78489973
1. 要获取当前系统是否支持FaceID或TouchID,一定要在

LAContext的canEvaluatePolicy条件下,否则常为空,简单说context还没有初始化。


//
//  ViewController.swift
//  FaceIDDemo
//
//  Created by axman.wang on 2017/11/9.
//  Copyright © 2017年 axman.wang. All rights reserved.
//

import UIKit
import LocalAuthentication


//AuthenticationFunctionClass
class AuthenticationWithFaceID{
    static let localizedReasonStr = "FaceID方式验证";
    static  func authenticate(complate:@escaping(Error?)->()){
        let context = LAContext()
    
        var authError: NSError? = nil
        
        if(context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError)){
            if context.biometryType != .typeFaceID {
                complate(NSError(domain:"AuthenticationWithFaceID.authenticate",code:0,userInfo:["result":"FaceID is not supported!"]))
                return
            }
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,localizedReason: localizedReasonStr,reply: {(success, error) -> Void in
                if success {
                    complate(nil)
                }else{
                    complate(error)
                }
            })
        }else{
            complate(authError)
        }
        
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var faceidBnt: UIButton!
    @IBAction func onBntPress(_ sender: UIButton) {
        AuthenticationWithFaceID.authenticate{(error) in
            if error == nil{
                let alert = UIAlertController(title: "提示", message: "认证成功!", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                alert.addAction(okAction)
                self.present(alert, animated: true, completion: nil)
            } else{
                let alert = UIAlertController(title: "提示", message: error?.localizedDescription, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                alert.addAction(okAction)
                self.present(alert, animated: true, completion: nil)
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }


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


}




2.一定要在info.plist中设置Privacy-Face ID Usage Description,否则一直提示该应用没有为FaceID优化。


猜你喜欢

转载自blog.csdn.net/axman/article/details/78489973
今日推荐