//導入頭檔案
#import <LocalAuthentication/LocalAuthentication.h>
//在app啟動的時候加載
- (void)evaluateAuthenticate
{
//建立LAContext
LAContext* context = [[LAContext alloc] init];
NSError* error = nil;
NSString* result = @"請驗證已有指紋";
//首先使用canEvaluatePolicy 判斷裝置支援狀态
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支援指紋驗證
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"驗證成功,主線程處理UI");
}
else
{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"系統取消授權,如其他APP切入");
break;
}
case LAErrorUserCancel:
{
NSLog(@"使用者取消驗證Touch ID");
break;
}
case LAErrorAuthenticationFailed:
{
NSLog(@"授權失敗");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"系統未設定密碼");
break;
}
case LAErrorTouchIDNotAvailable:
{
NSLog(@"裝置Touch ID不可用,例如未打開");
break;
}
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"裝置Touch ID不可用,使用者未錄入");
break;
}
case LAErrorUserFallback:
{
NSLog(@"使用者選擇輸入密碼,切換主線程處理");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"其他情況,切換主線程處理");
}];
break;
}
}
}
}];
}
else
{
//不支援指紋識别,LOG出錯誤詳情
NSLog(@"不支援指紋識别==%ld",(long)error.code);
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"身份驗證無法啟動,因為Touch ID沒有注冊手指");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"身份驗證不能啟動,因為沒有在裝置上設定密碼");
break;
}
default:
{
NSLog(@"TouchID not available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
}
}