指纹密码验证处理 还有用户主动点击取消是在子线程,这时候去突出一个界面,会崩溃,因为一个新的界面创建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)
}
}
}