版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.02.01 |
前言
LocalAuthentication
是用来实现iOS中的指纹识别的,自从iPhone5s加入TouchID后,LocalAuthentication也越来越受到关注。接下来我们就一起看一下这个框架的使用。
Overview
首先看一下该框架的基本信息
LocalAuthentication,通过密码或生物识别技术请求用户的身份验证,比较典型的就是TouchID。
LocalAuthentication
框架提供了用于从具有指定安全策略的用户请求认证的工具。
例如,要请求用户仅使用Face ID或Touch ID进行认证,您可以使用如下代码:
OC版本
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = <#String explaining why app needs authentication#>;
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
// User authenticated successfully, take appropriate action
} else {
// User did not authenticate successfully, look at error and take appropriate action
}
}];
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
}
Swift版本
let myContext = LAContext()
let myLocalizedReasonString = <#String explaining why app needs authentication#>
var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
if success {
// User authenticated successfully, take appropriate action
} else {
// User did not authenticate successfully, look at error and take appropriate action
}
}
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
}
} else {
// Fallback on earlier versions
}
下面我们看一个该框架的详细结构。
Topics
1. Classes
-
LAContext
- 一个
LAContext
对象表示一个认证上下文。 LAContext类提供了一个编程接口,用于评估认证策略和访问控制,管理凭证和使认证上下文失效。
- 一个
2. Reference
后记
本篇已结束,后面更精彩~~~~