iOS8之後蘋果發布了指紋識別的功能,通過touch ID來識別用戶,做用戶授權,主要是依賴於LocalAuthentication庫
指紋識別:一判斷設備是否支持指紋識別功能
二識別指紋,成功後做相應的動作,失敗後提醒用戶指紋識別失敗
先引入#import <LocalAuthentication/LocalAuthentication.h>
LAContext *context = [[LAContext alloc] init]; NSError *error = nil; //驗證是否支持指紋識別 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { NSLog(@"支持指紋識別的應用"); //驗證身份 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"需要指紋來驗證你的身份", @"hello") reply:^(BOOL success, NSError *error) { if (success) { NSLog(@"成功"); } else { switch (error.code) { case LAErrorUserCancel: NSLog(@"用戶取消了授權 - %@", error.localizedDescription); break; case LAErrorUserFallback: NSLog(@"用戶點擊了“輸入密碼”按鈕 - %@", error.localizedDescription); break; case LAErrorAuthenticationFailed: NSLog(@"您已授權失敗3次 - %@", error.localizedDescription); break; case LAErrorTouchIDLockout: NSLog(@"指紋被鎖定 - %@", error.localizedDescription); break; case LAErrorSystemCancel: NSLog(@"應用程序進入後台 - %@", error.localizedDescription); break; default: NSLog(@"++%@--%zd", error.localizedDescription, error.code); break; } } }]; } else { switch (error.code) { case LAErrorPasscodeNotSet: NSLog(@"未設置密碼 - %@", error.localizedDescription); break; case LAErrorTouchIDNotEnrolled: NSLog(@"未注冊 Touch ID - %@", error.localizedDescription); break; case kLAErrorTouchIDNotAvailable: NSLog(@"該設備不支持 Touch ID - %@", error.localizedDescription); break; default: NSLog(@"--%@--%zd", error.localizedDescription, error.code); break; } }