iOS开发swift -- 指纹识别

代码示例

    //导入类库 import LocalAuthentication
    func touchID() {
        //创建上下文
        let context = LAContext()
        var error : NSError?
        //判断设备是否支持指纹识别
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
        {
            //开始指纹识别
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "请用指纹解锁", reply: { (success, error) in
                if success {
                    debugPrint("successful")
                    //do what you want
                }else
                {
                    if let error = error as NSError?
                    {
                        switch error.code {
                        case LAError.appCancel.rawValue:
                            debugPrint("Authentication was cancelled by application")
                            
                        case LAError.authenticationFailed.rawValue:
                            debugPrint("The user failed to provide valid credentials")
                            
                        case LAError.invalidContext.rawValue:
                            debugPrint("The context is invalid")
                            
                        case LAError.passcodeNotSet.rawValue:
                            debugPrint("Passcode is not set on the device")
                            
                        case LAError.systemCancel.rawValue:
                            debugPrint("Authentication was cancelled by the system")
                            
                        case LAError.touchIDLockout.rawValue:
                            debugPrint("Too many failed attempts.")
                            
                        case LAError.touchIDNotAvailable.rawValue:
                            debugPrint("TouchID is not available on the device")
                            
                        case LAError.userCancel.rawValue:
                            debugPrint("The user did cancel")
                            
                        case LAError.userFallback.rawValue:
                            debugPrint("The user chose to use the fallback")
                            
                        default:
                            debugPrint( "Did not find error code on LAError object")
                        }
                    }
                }
            })
        }else
        {
            debugPrint("no support")
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 介绍 Robolectric 测试框架针对 Android 的组件(包含各种View)进行了统一的 Shadow,...
    alighters阅读 7,417评论 1 7
  • 演出结束之后,宫响带长月去了附近的电玩城,说是让长月见识见识最新型电子游戏,可到了那里宫响就直奔游戏而去,彻底的将...
    煦子阅读 1,787评论 0 2
  • 缘分那么奇妙,让你遇到的人,和你走向完全不同的人生道路。 在那个平凡的农村里,家家户户都不富裕,教育落后。小清和小...
    二风阅读 3,698评论 1 3
  • 你好吗? 你最喜欢的夏天 已经涨满了蓝色的风 过不了多久 我们 就要向蝉鸣投降啦 我们还会再见面吗?
    冷康康阅读 1,085评论 3 3