你好,歡迎來到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這兩個方式來完成,感興味的話可以看下。

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

【iOS指紋驗證TouchID使用學習教程】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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