iOS系統的指紋識別功能最低支持的機型爲iPhone 5s
,最低支持系統爲iOS 8
,雖然安裝iOS 7系統的5s
機型能夠使用系統提供的指紋解鎖功能,但因爲API
並未開放,因此理論上第三方軟件不可以使用。ios
LocalAuthentication.framework #import <LocalAuthentication/LocalAuthentication.h>
作iOS 8如下
版本適配時,務必進行API驗證
,避免調用相關API引發崩潰。app
if(iOS8){xxx} // 系統版本驗證 if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError])
LAContext
指紋驗證操做對象框架
#import "ViewController.h" #import <LocalAuthentication/LocalAuthentication.h> #define iOS8later [UIDevice currentDevice].systemVersion.doubleValue >= 8.0 @interface ViewController () @end @implementation ViewController - (IBAction)showTouchIDAlert:(id)sender { // 1.判斷是不是iOS8以後 if (!iOS8later) { NSLog(@"版本不對不能使用TouchID"); return; } // 2.調用touchID的相關方法 [self authenticateUser]; } // 鑑定用戶 - (void)authenticateUser { // 建立指紋驗證對象 LAContext *context = [[LAContext alloc] init]; NSError *yfError = nil; // 驗證設備是否支持touchID // LAPolicyDeviceOwnerAuthenticationWithBiometrics 14年時候枚舉只有這一個屬性 // LAPolicyDeviceOwnerAuthentication 後來加的枚舉屬性 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&yfError]) { // 支持touchID [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"XMGlocalizedReason" reply:^(BOOL success, NSError * _Nullable error) { if (success) { // touchID驗證成功 NSLog(@"touchID驗證成功"); // 繼續處理相關業務(注意線程) }else { NSLog(@"%@",error.localizedDescription); switch (error.code) { case LAErrorSystemCancel: { NSLog(@"Authentication was cancelled by the system"); //切換到其餘APP,系統取消驗證Touch ID break; } case LAErrorUserCancel: { NSLog(@"Authentication was cancelled by the user"); //用戶取消驗證Touch ID break; } case LAErrorUserFallback: { NSLog(@"User selected to enter custom password"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //用戶選擇輸入密碼,切換主線程處理 }]; break; } default: { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //其餘狀況,切換主線程處理 }]; break; } } } }]; }else { switch (yfError.code) { case LAErrorTouchIDNotEnrolled: NSLog(@"LAErrorTouchIDNotEnrolled"); break; case LAErrorPasscodeNotSet: NSLog(@"LAErrorPasscodeNotSet"); // 此處觸發showPasscodeResetAlert方法 break; default: NSLog(@"Touch ID is unaviliable"); break; } NSLog(@"%@", yfError.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); */ - (void)viewDidLoad { [super viewDidLoad]; } @end
前輩Demo:兩行代碼集成TouchID指紋驗證lua