Skip to content

Instantly share code, notes, and snippets.

@muhlenXi
Last active January 25, 2019 08:54
Show Gist options
  • Save muhlenXi/6e61d2261dd1da3e9b402269898c0623 to your computer and use it in GitHub Desktop.
Save muhlenXi/6e61d2261dd1da3e9b402269898c0623 to your computer and use it in GitHub Desktop.
iOS 指纹或面部识别
import LocalAuthentication
@objc func onClickPayBtn(_ sender: UIButton) {
let context = LAContext()
var error: NSError?
let isEnable = context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error)
if isEnable == false {
if let err = error {
var message = ""
switch err.code {
case LAError.appCancel.rawValue:
message = "Authentication was cancelled by application"
case LAError.authenticationFailed.rawValue:
message = "The user failed to provide valid credentials"
/// 多次验证失败
case LAError.invalidContext.rawValue:
message = "The context is invalid"
case LAError.passcodeNotSet.rawValue:
message = "Passcode is not set on the device"
case LAError.systemCancel.rawValue:
message = "Authentication was cancelled by the system"
case LAError.touchIDLockout.rawValue:
message = "Too many failed attempts."
/// 多次尝试失败
case LAError.touchIDNotAvailable.rawValue:
message = "TouchID is not available on the device"
case LAError.userCancel.rawValue:
message = "The user did cancel"
case LAError.userFallback.rawValue:
message = "The user chose to use the fallback"
/// 这里弹出输入密码页面
case LAError.touchIDNotEnrolled.rawValue:
message = "touchIDNotEnrolled"
/// 未设置指纹或
default:
message = "Did not find error code on LAError object"
}
print(message)
}
} else {
print("支持生物识别")
}
if isEnable == true && error == nil {
context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "请验证已有的指纹,用于支付") { (result, error) in
if error == nil, result == true {
print("生物信息识别成功")
} else {
if error != nil {
let err = error! as NSError
var message = ""
switch err.code {
case LAError.appCancel.rawValue:
message = "Authentication was cancelled by application"
case LAError.authenticationFailed.rawValue:
message = "The user failed to provide valid credentials"
/// 多次验证失败
case LAError.invalidContext.rawValue:
message = "The context is invalid"
case LAError.passcodeNotSet.rawValue:
message = "Passcode is not set on the device"
case LAError.systemCancel.rawValue:
message = "Authentication was cancelled by the system"
case LAError.touchIDLockout.rawValue:
message = "Too many failed attempts."
case LAError.touchIDNotAvailable.rawValue:
message = "TouchID is not available on the device"
case LAError.userCancel.rawValue:
message = "The user did cancel"
case LAError.userFallback.rawValue:
message = "The user chose to use the fallback"
/// 这里弹出输入密码页面
default:
message = "Did not find error code on LAError object"
}
print(message)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment