你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS指紋驗證TouchID應用學習教程

iOS指紋驗證TouchID應用學習教程

編輯:IOS開發綜合

 指紋驗證這個功能現在在一些app中經常常見,常常與數字解鎖,手勢解鎖聯合起來使用。前幾天接到說實現一個指紋驗證的功能,搗鼓了挺久,然後今天,我就簡單的介紹下指紋驗證,會做個簡單的demo實現一下基本的功能。
    支持系統和機型:iOS系統的指紋識別功能最低支持的機型為iPhone 5s,最低支持系統為iOS 8。實現起來呢,其實還是很簡單的,下面我們就用純代碼方式實現一個簡單的demo1。

第一部分:調用原生服務實現指紋驗證

這部分了解個大概就可以了

第一步:添加LocalAuthentication.framework庫

第二步:在appdelegate.m中添加代碼
這個不說其實大家也都知道的吧。

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 //appdelegate
 _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
 _window.backgroundColor = [UIColor whiteColor];
 [_window makeKeyAndVisible];
 ViewController *vc = [[ViewController alloc]init];
 UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:vc];
 _window.rootViewController = na;
 return YES;
}

第三步
引入頭文件

  #import <LocalAuthentication/LocalAuthentication.h>

第四步:實現指紋驗證 

這一步就是很重要的地方了,在- (void)viewDidLoad中寫入驗證實現的代碼,這裡只有兩步,因為LAContext在官方文檔中只有兩個方法:

 -canEvaluatePolicy:error: 
//-(BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));

 -evaluatePolicy:localizedReason:reply: 
//- (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError * __nullable error))reply;

一個是判斷設備是否支持touchid,一個是進行驗證返回不同的結果,之前在網上經常可以一些文章中寫了,指紋驗證的第一步都是先判斷設備的系統版本等等,現在似乎都不需要了,只要調用該方法就可以了。全部的代碼 如下:

- (void)viewDidLoad {
 [super viewDidLoad];
 self.title = @"TouchIDSimpleDemoOne";
 LAContext *context = [[LAContext alloc]init];
 NSError *error;
 NSString *result = @"需要你身份驗證呢"; 
 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error])
 {
 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error)
 {
  if (success)
  {
  //驗證成功,主線程處理UI
  //這個地方呢就是寫一些驗證成功之後需要做些什麼事情的代碼。
  NSLog(@"驗證成功");
  }
  else
  {
  //以下是一些驗證失敗的原因啥的
  NSLog(@"%@",error.localizedDescription);
  switch (error.code) {
   case LAErrorSystemCancel:
   {
   NSLog(@"切換到其他APP,系統取消驗證Touch ID");
   //切換到其他APP,系統取消驗證Touch ID
   break;
   }
   case LAErrorUserCancel:
   {
   NSLog(@"用戶取消驗證Touch ID");
   //用戶取消驗證Touch ID
   break;
   }
   case LAErrorUserFallback:
   {
   NSLog(@"用戶選擇輸入密碼");
   [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    //用戶選擇其他驗證方式,切換主線程處理
   }];
   break;
   }
   default:
   {
   NSLog(@"LAErrorAuthenticationFailed,授權失敗");
   //授權失敗
   [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    //其他情況,切換主線程處理
   }];
   break;
   }
  }
  }
 }]; 
 }else
 {
 //不支持指紋識別,LOG出錯誤詳情

 switch (error.code) {
  case LAErrorTouchIDNotEnrolled:
  {
  NSLog(@"設備Touch ID不可用,用戶未錄入");
  break;
  }
  case LAErrorPasscodeNotSet:
  {
  NSLog(@"系統未設置密碼");
  break;
  }
  case LAErrorTouchIDNotAvailable:
  {
  NSLog(@"設備Touch ID不可用,例如未打開");
  break;
  }
  default:
  {
  NSLog(@"系統未設置密碼");
  break;
  }
 }
 NSLog(@"%@",error.localizedDescription);
 } 
}

//指紋驗證返回值
typedef NS_ENUM(NSInteger, LAError)
{
 /// Authentication was not successful, because user failed to provide valid credentials.
 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,

 /// Authentication was canceled by user (e.g. tapped Cancel button).
 LAErrorUserCancel  = kLAErrorUserCancel,

 /// Authentication was canceled, because the user tapped the fallback button (Enter Password).
 LAErrorUserFallback  = kLAErrorUserFallback,

 /// Authentication was canceled by system (e.g. another application went to foreground).
 LAErrorSystemCancel  = kLAErrorSystemCancel,

 /// Authentication could not start, because passcode is not set on the device.
 LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,

 /// Authentication could not start, because Touch ID is not available on the device.
 LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,

 /// Authentication could not start, because Touch ID has no enrolled fingers.
 LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,

 /// Authentication was not successful, because there were too many failed Touch ID attempts and
 /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating
 /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.
 LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout,

 /// Authentication was canceled by application (e.g. invalidate was called while
 /// authentication was in progress).
 LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,

 /// LAContext passed to this call has been previously invalidated.
 LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
} NS_ENUM_AVAILABLE(10_10, 8_0);

以上呢,就是一個簡單的demo了,可能有些小問題,到時候需要的話可以自調整。這裡附上這個demo的guithub鏈接看這裡看這裡,鏈接在這呢。

第二部分:利用現有的第三方組件實現

這個部分可以好好學習一下。

    在這裡呢,我要推薦一個別人寫的一個第三方的組件,就是[WJTouchID](https://github.com/hu670014125/WJTouchID);這個控件的話,在這個鏈接上其實已經有寫出怎麼用了,其實不需要我再都說什麼,但是我還是要說下吧。
    調用時只需要一兩行代碼調用,但是回調函數還是需要寫不少東西的。。。

1:復制文件進去

2:引入頭文件
#import "WJTouchID.h"

3:遵守協議
@interface ViewController ()<WJTouchIDDelegate>

4: 創建對象
@property (nonatomic, strong) WJTouchID *touchID;
5:調用

- (void)viewDidLoad {
 [super viewDidLoad];
 //初始化
 WJTouchID *touchid = [[WJTouchID alloc]init];
 [touchid startWJTouchIDWithMessage:WJNotice(@"自定義信息", @"The Custom Message") fallbackTitle:WJNotice(@"", @"Fallback Title") delegate:self];
 self.touchID = touchid;
}

6:實現回調函數

@required

 //TouchID驗證成功
- (void)WJTouchIDAuthorizeSuccess;

 //TouchID驗證失敗
- (void)WJTouchIDAuthorizeFailure;

@optional

 //當前設備不支持指紋識別
- (void)WJTouchIDIsNotSupport;

 //當前軟件被掛起取消了授權(如突然來了電話,應用進入前台)
- (void)WJTouchIDAuthorizeErrorAppCancel;

 //取消TouchID驗證 (用戶點擊了取消)
- (void)WJTouchIDAuthorizeErrorUserCancel;

 //在TouchID對話框中點擊輸入密碼按鈕
- (void)WJTouchIDAuthorizeErrorUserFallback;

 //在驗證的TouchID的過程中被系統取消 例如突然來電話、按了Home鍵、鎖屏...
- (void)WJTouchIDAuthorizeErrorSystemCancel;

 //無法啟用TouchID,設備沒有設置密碼
- (void)WJTouchIDAuthorizeErrorPasscodeNotSet;

 //多次連續使用Touch ID失敗,Touch ID被鎖,需要用戶輸入密碼解鎖
- (void)WJTouchIDAuthorizeErrorTouchIDLockout;

 //當前軟件被掛起取消了授權 (授權過程中,LAContext對象被釋)
- (void)WJTouchIDAuthorizeErrorInvalidContext;

 //設備沒有錄入TouchID,無法啟用TouchID
- (void)WJTouchIDAuthorizeErrorTouchIDNotEnrolled;

 //該設備的TouchID無效
- (void)WJTouchIDAuthorizeErrorTouchIDNotAvailable;

這些方法實現結束後呢,這個功能也基本上算是完成了。因為好像篇幅太長了,看得人肯定也嫌煩,所以我准備另寫一篇做一個在app被喚醒的時候啟動指紋驗證,分別用彈出控制器和彈出自定義view這兩個方式來實現,感興趣的話可以看下。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved