指纹密码swift版

 

   指纹密码验证处理 还有用户主动点击取消是在子线程,这时候去突出一个界面,会崩溃,因为一个新的界面创建UI的时候需要在主线程里面进行;


   //MARK: - 开启指纹登录

    func fingerprintLogin(on:Bool)  {

        //1.初始化TouchID句柄

        let authentication = LAContext()

        authentication.localizedFallbackTitle = ""

        var error: NSError?

        

        //2.检查Touch ID是否可用

        let isAvailable = authentication.canEvaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics,

                                                           error: &error)

        

        //3.处理结果

        if isAvailable

        {

            NSLog("Touch ID is available")

            //这里是采用认证策略 LAPolicy.DeviceOwnerAuthenticationWithBiometrics

            //--> 指纹生物识别方式

            authentication.evaluatePolicy(.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "请验证指纹", reply: {

                //当调用authentication.evaluatePolicy方法后,系统会弹提示框提示用户授权

                (success, error) -> Void in

                if success

                {

                    NSLog("您通过了Touch ID指纹验证!")

                    if on == true {

                        UserInfo.openFigerLogin()

                        Utils.showAlert(self, title: "指纹验证成功", message: "您已成功开启指纹登录", btnTitle: "好的",finished: {

                            self.tableView.reloadData()

                        })

                    }else {

                        UserInfo.closeFigerLogin()

                        Utils.showAlert(self, title: "指纹验证成功", message: "您已成功关闭指纹登录", btnTitle: "好的",finished: {

                            self.tableView.reloadData()

                        })

                        print("指纹密码已关闭")

                    }

                    

                }

                else

                {

//                    self.isNeedOn = true

//                    self.fingerprintSwitchChanged(self.fingerSwitch!)


                    //上面提到的指纹识别错误

                    NSLog("您未能通过Touch ID指纹验证!错误原因:\n\(error)")

                    if error!.code == -1 {

                        let alert = UIAlertController(title: "指纹验证失败!", message: "您的指纹验证超过次数限制", preferredStyle: UIAlertControllerStyle.Alert)

                        alert.addAction(UIAlertAction(title: "知道了", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) in

                            

                        }))

                        self.presentViewController(alert, animated: true, completion: nil)

                    }else if error!.code == -2 {

                        print("用户主动取消了指纹验证")

                        

                    }else if error!.code == -1000 {

                        let alert = UIAlertController(title: "指纹验证失败!", message: "模拟器没有Touch ID 功能", preferredStyle: UIAlertControllerStyle.Alert)

                        alert.addAction(UIAlertAction(title: "知道了", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) in

                        }))

                    }else if error!.code == -4 {

                      print("error -- >"+"\(error)")

                    }else {

                        let alert = UIAlertController(title: "指纹验证失败!", message: "您未能通过Touch ID指纹验证!错误原因:\n\(error)", preferredStyle: UIAlertControllerStyle.Alert)

                        alert.addAction(UIAlertAction(title: "知道了", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) in

                            

                        }))

                        self.presentViewController(alert, animated: true, completion: nil)

                    }

                }

            })

        }

        else

        {

            

            //上面提到的硬件配置

            NSLog("Touch ID不能使用!错误原因:\n\(error)")

            if error?.code == -7 {

                let alert = UIAlertController(title: "开启指纹登录失败!", message: "没有发现可用的Touch ID,请确保您已开启设备的Touch ID功能", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "知道了", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) in

                    

                }))

                self.presentViewController(alert, animated: true, completion: nil)

            }else if error?.code == -6 {

                let alert = UIAlertController(title: "开启指纹登录失败!", message: "您的设备不支持Touch ID", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "知道了", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) in

                    

                }))

                self.presentViewController(alert, animated: true, completion: nil)

            }else {

            

            let alert = UIAlertController(title: "开启指纹登录失败!", message: "Touch ID不能使用!", preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "知道了", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) in

                

            }))

            self.presentViewController(alert, animated: true, completion: nil)

            }

        }

        

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值