標籤:
添加依賴庫 LocalAuthentication.framework
#import <LocalAuthentication/LocalAuthentication.h> // 標頭檔
LAContext *context = [[LAContext alloc] init];
NSError *error = nil;
// 這句代碼是讓指紋驗證的提示框沒有輸入密碼
context.localizedFallbackTitle = @"";
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請通過指紋驗證解鎖應用" reply:^(BOOL success, NSError * _Nullable error) {
if (success) { // 驗證成功
// 指紋解鎖是在子線程執行的,當你要重新整理UI的時候需要回到主線程,要不然就會看到介面卡死
dispatch_async(dispatch_get_main_queue(), ^{
self.dactylogramLbl.text = self.dactylogramSwitch.on ? @"指紋驗證已開啟" : @"指紋驗證未開啟";
[kUserDefaults setBool:self.dactylogramSwitch.on forKey:@"isOpenFingerprintPwd"];
});
} else {
if (error.code == kLAErrorUserCancel) { // 點擊了取消
NSLog(@"驗證失敗3");
} else {
NSLog(@"驗證失敗2");
}
} else {
NSLog(@"您的裝置不能使用 TouchID 進行身分識別驗證");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的裝置不能使用 TouchID 進行身分識別驗證" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alertView show];
}
iOS9 整合指紋解鎖