1、首先导入本地验证库(LocalAuthentication)
import LocalAuthentication
2、验证是否设备是否支持指纹或FaceID
context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)
3、若支持,则开始申请验证
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Log in to your account"
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in
if success {
// Move to the main thread because a state update triggers UI changes.
DispatchQueue.main.async { [unowned self] in
self.state = .loggedin
}
} else {
print(error?.localizedDescription ?? "Failed to authenticate")
// Fall back to a asking for username and password.
// ...
}
}
} else {
print(error?.localizedDescription ?? "Can't evaluate policy")
// Fall back to a asking for username and password.
// ...
}
4、验证失败的原因(这个一般基本上都不会出现,所以直接都当错误处理了)
func authFail(err: Error?) {
if let c = err as NSError?{
let cc = LocalAuthentication.LAError.Code.init(rawValue: c.code)
switch(cc) {
case LocalAuthentication.LAError.systemCancel:
print("系统取消授权,如其他APP切入");
break;
case LocalAuthentication.LAError.userCancel:
print("用户取消验证Touch ID");
break;
case LocalAuthentication.LAError.authenticationFailed:
print("授权失败");
break;
case LocalAuthentication.LAError.passcodeNotSet:
print("系统未设置密码");
break;
case LocalAuthentication.LAError.biometryNotAvailable:
print("设备Touch ID不可用,例如未打开");
break;
case LocalAuthentication.LAError.biometryNotEnrolled:
print("设备Touch ID不可用,用户未录入");
break;
case LocalAuthentication.LAError.userFallback:
print("用户选择输入密码,切换主线程处理");
break;
default:
print("其他情况,切换主线程处理");
break;
}
}
}
5、苹果官方完整示例代码
https://download.csdn.net/download/wangkunggxx/87391697
原文地址:https://blog.csdn.net/wangkunggxx/article/details/128729057
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_24254.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。