美文网首页
iOS Touch ID(指纹识别)开发

iOS Touch ID(指纹识别)开发

作者: JQWONG | 来源:发表于2019-07-16 17:13 被阅读0次
    简介

    iPhone 5s~iPhone 8都可以通过指纹进行解锁,省去了反复输入密码的繁琐程序,这个就是Touch ID指纹识别技术。在iOS 8以后,Apple也开放了Touch ID的API给开发者,因此很多App也引入了这个新的登录方式。Face ID实现过程与Touch ID基本相同,后期也会再做补充


    开发应用
    • 要实现指纹识别的功能需要引入LocalAuthentication框架
    • 首先我们需要判断目前设备是否支持Touch ID功能
    func canEvaluatePolicy(_ policy: LAPolicy, error: NSErrorPointer) -> Bool
    
    • LAPolicy有两个值,分别是deviceOwnerAuthenticationWithBiometricsdeviceOwnerAuthentication
    • deviceOwnerAuthenticationWithBiometrics:用手指指纹去验证,iOS8.0以上可用
    • deviceOwnerAuthentication:使用TouchID或者密码验证,默认是错误三次指纹或者锁定后,弹出输入密码界面iOS 9.0以上可用
    • 开始指纹验证
    func evaluatePolicy(_ policy: LAPolicy, localizedReason: String, reply: @escaping (Bool, Error?) -> Void)
    

    主要代码

        func evaluate(reason: String, completed: @escaping (_ isSuccess: Bool)->())  {
            //记录是否开启Touch ID
            var canTouchID = true
            canTouchID = content.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
    
            guard canTouchID else {
                completed(false)
                return
            }
            //本地认证上下文联系对象,每次使用Touch ID功能都要重新初始化
            content = nil
            content = LAContext()
    
            guard let content = content else { return }
            //调起Touch ID验证弹框
            content.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: “指纹验证登录”) {
                (success, error) in
                //成功与失败进行相应的操作
                if success {
                    completed(true)
                }else {
                    completed(false)
                    print("\(error!)")
                    if let errorCode = LAError.Code(rawValue: (error! as NSError).code) {
                      switch errorCode
                        //对应errorCode的相应操作
                        ……
                    }
                }
            }
        }
    

    localizedReason:用于设置提示语,表示为什么要使用Touch ID

    常见erro描述
    • 在模拟器上运行结果
    Error Domain=com.apple.LocalAuthentication Code=-1000 "Simulator is not supported." 
    UserInfo=0x7ffe604b0790 {NSLocalizedDescription=Simulator is not supported.
    
    • 在有TouchID 的功能下,没有设置TouchID的运行结果
    Error Domain=com.apple.LocalAuthentication Code=-7 "No fingers are enrolled with Touch ID." 
    UserInfo=0x170075fc0 {NSLocalizedDescription=No fingers are enrolled with Touch ID.
    
    • 连续三次指纹识别错误
    Error Domain=com.apple.LocalAuthentication Code=-1 "Aplication retry limit exceeded." 
    UserInfo=0x1740797c0 {NSLocalizedDescription=Aplication retry limit exceeded.}
    
    • 用户在Touch ID对话框中点击了取消按钮
    Error Domain=com.apple.LocalAuthentication Code=-2 "Canceled by user." 
    UserInfo=0x17006c780 {NSLocalizedDescription=Canceled by user.
    
    • Touch ID三次校验失败
    Error Domain=com.apple.LocalAuthentication Code=-1 "Application retry limit exceeded." 
    UserInfo={NSLocalizedDescription=Application retry limit exceeded.}
    
    • Touch ID多次校验失败
    Error Domain=com.apple.LocalAuthentication Code=-8 "Biometry is locked out." 
    UserInfo={NSLocalizedDescription=Biometry is locked out.
    

    参考文章
    https://www.jianshu.com/p/aef5a506311b
    https://juejin.im/entry/59795ee95188253e000a74c3

    相关文章

      网友评论

          本文标题:iOS Touch ID(指纹识别)开发

          本文链接:https://www.haomeiwen.com/subject/pddtlctx.html