Swift验证指纹解锁
2018-03-21 本文已影响0人
向日葵的夏天_summer
在支付宝中有看到这种效果,苹果在IOS8之后就已经封装好了这个功能,只需要调用它提供的接口就行了:
IMG_2844.PNG
具体实现的代码:
class MyFingureCheckTool: NSObject {
static func myFingureAuthentication(withTips tips: String = "验证指纹", block: @escaping (_ result: FingureCheckResult) -> () ) {
guard #available(iOS 8.0, *) else {
block(FingureCheckResult.touchIDNotAvaliable)
return
}
let context = LAContext()
var error: NSError? = nil
if context.canEvaluatePolicy(LAPolicy(rawValue: Int(kLAPolicyDeviceOwnerAuthenticationWithBiometrics))!, error: &error) {
context.evaluatePolicy(LAPolicy(rawValue: Int(kLAPolicyDeviceOwnerAuthenticationWithBiometrics))!, localizedReason: tips, reply: { (success, error) in
if success {
block(FingureCheckResult.success)
print("指纹验证成功")
}else {
let laError = error as! LAError
switch laError.code {
case LAError.authenticationFailed:
block(FingureCheckResult.fialed)
print("连续三次输入错误,身份验证失败")
case LAError.userCancel:
block(FingureCheckResult.userCancel)
print("用户点击取消按钮")
case LAError.userFallback:
block(FingureCheckResult.inputPassword)
print("用户点击输入密码")
case LAError.systemCancel:
block(FingureCheckResult.systemCancel)
print("系统取消")
case LAError.passcodeNotSet:
block(FingureCheckResult.passwordNotSet)
print("用户未设置密码")
case LAError.touchIDNotAvailable:
block(FingureCheckResult.touchIDNotAvaliable)
print("touchID不可用")
case LAError.touchIDNotEnrolled:
block(FingureCheckResult.touchIDNotSet)
print("touchID未设置指纹")
default:
break
}
}
})
}else {
print("不支持TouchID")
}
}
}
还有一个枚举:
enum FingureCheckResult {
case success //成功
case fialed //失败
case passwordNotSet //未设置手机密码
case touchIDNotSet //未设置指纹
case touchIDNotAvaliable //不支持指纹
case systemCancel //系统取消
case userCancel //用户取消
case inputPassword //输入密码
}
然后在调用的时候:
MyFingureCheckTool.myFingureAuthentication { (result) in
switch result {
case .success:
print("成功")
case .fialed:
print("失败")
case .passwordNotSet:
print("未设置密码")
case .touchIDNotSet:
print("未设置指纹")
case .touchIDNotAvaliable:
print("系统不支持")
default:
break
}
}