1、引入头文件
import LocalAuthentication
2、声明一个枚举
enum TouchIdCase: Int {
case success
case failure
case passwordNoSet
case touchIdNoSet
case touchIdNoAvailable
case systemCancle
case userCancel
case inputPassword
}
3、代码的实现
class func touchId(toast : String, completion : @escaping (_ result :TouchIdCase) -> Void) {
if #available(iOS 8.0, OSX 10.12, *) {
let context = LAContext()
var error: NSError? = nil
if(context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error)) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason:toast, reply: { (success, error) in
DispatchQueue.main.async {
if(success) {
completion(.success)
} else {
let laError = error as! LAError
errorData(laError: laError, completion: completion)
}
}
})
} else {
let laError = error as! LAError
errorData(laError: laError, completion: completion)
}
}
}
class func errorData(laError : LAError, completion : @escaping (_ result :TouchIdCase) -> Void){
switch laError.code {
case LAError.authenticationFailed:
completion(.failure)
break
case LAError.userCancel:
completion(.userCancel)
break
case LAError.userFallback:
completion(.inputPassword)
break
case LAError.systemCancel:
completion(.systemCancle)
break
case LAError.passcodeNotSet:
completion(.passwordNoSet)
break
case LAError.touchIDNotAvailable:
completion(.touchIdNoAvailable)
break
case LAError.touchIDNotEnrolled:
completion(.touchIdNoAvailable)
break
default: break
}
}
4、注意
a、deviceOwnerAuthenticationWithBiometrics是支持iOS8以上系统,使用该设备的TouchID进行验证,当输入TouchID验证5次失败后,TouchID被锁定,只能通过锁屏后解锁设备时输入正确的解锁密码来解锁TouchID。
b、 deviceOwnerAuthentication 是支持iOS9以上系统,使用该设备的TouchID或设备密码进行验证,当输入TouchID验证5次失败后,TouchID被锁定,会触发设备密码页面进行验证。
网友评论