轉載自:http://blog.csdn.net/hongfengkt/article/details/49868073
在iOS8.0之後,API開放了指紋驗證的功能。
指紋驗證功能的最低硬件支持爲iPhone5s,iPad 6,iPad mini 3這些有touch ID硬件支持的設備,並且操作系統最低爲iOS8.0,因爲touch ID是在iOS8.0之後纔開放的一類api。
———-做iOS8.0下版本適配時,務必進行API驗證,避免調用相關API引起崩潰。
引入框架:#import
- (void)authenticateUser
{
//初始化上下文對象
LAContext *context = [[LAContext alloc]init];
NSError *error = nil;
NSString *result = @"Authentication is needed to access your notes.";
//首先使用canEvaluatePolocy判斷設備支持狀態
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指紋驗證
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
//驗證成功,主線程處理UI
NSLog(@"指紋驗證成功");
}
else
{
NSLog(@"指紋驗證失敗=====%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
//切換到其他APP,系統取消驗證Touch ID
break;
case LAErrorUserCancel:
//用戶取消驗證Touch ID
break;
case LAErrorUserFallback:
{
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
//用戶選擇其他驗證方式,切換到主線程處理UI
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情況,切換主線程處理
}];
}
break;
}
}
}];
}
else
{
//不支持指紋識別,LOG出錯誤詳情
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
//未錄入Touch ID
break;
case LAErrorPasscodeNotSet:
//未設置Touch ID
break;
default:
//Touch ID不可用
break;
}
NSLog(@"%@",error.localizedDescription);
}
}
有以下幾種情況
typedef NS_ENUM(NSInteger, LAError)
{
//授權失敗
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
//用戶取消Touch ID授權
LAErrorUserCancel = kLAErrorUserCancel,
//用戶選擇輸入密碼
LAErrorUserFallback = kLAErrorUserFallback,
//系統取消授權(例如其他APP切入)
LAErrorSystemCancel = kLAErrorSystemCancel,
//系統未設置密碼
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
//設備Touch ID不可用,例如未打開
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
//設備Touch ID不可用,用戶未錄入
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);