iOS開發之指紋解鎖

來源:互聯網
上載者:User

標籤:

http://blog.csdn.net/hongfengkt/article/details/49868073

 

 

前一陣子一直在趕項目進度,沒有太多時間寫部落格,現在終於空閑了,將以前欠下的部落格補上來。 
在iOS8.0之後,API開放了指紋驗證的功能。開放的API雖然只能用來作為驗證使用,比較局限,但是對於有類似“手勢密碼”功能的app來說,已經足夠用了。有了這個新api,就可以在app中再多一種高大上的解鎖方式了。 
指紋驗證功能的最低硬體支援為iPhone5s,iPad 6,iPad mini 3這些有touch ID硬體支援的裝置,並且作業系統最低為iOS8.0,因為touch ID是在iOS8.0之後才開放的一類api。 
下面放代碼。 
做iOS8.0下版本適配時,務必進行API驗證,避免調用相關API引起崩潰。 
引入依賴架構:

#import <LocalAuthentication/LocalAuthentication.h>
  • 1
  • 1

指紋驗證的實現

- (void)authenticateUser{    //初始化內容物件    LAContext* context = [[LAContext alloc] init];    //錯誤對象    NSError* error = nil; NSString* result = @"Authentication is needed to access your notes."; //首先使用canEvaluatePolicy 判斷裝置支援狀態 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { //支援指紋驗證 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) { if (success) { //驗證成功,主線程處理UI } 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 { //不支援指紋識別,LOG出錯誤詳情 switch (error.code) { case LAErrorTouchIDNotEnrolled: { NSLog(@"TouchID is not enrolled"); break; } case LAErrorPasscodeNotSet: { NSLog(@"A passcode has not been set"); break; } default: { NSLog(@"TouchID not available"); break; } } NSLog(@"%@",error.localizedDescription); [self showPasswordAlert]; }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

對幾種情況的說明

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);

iOS開發之指紋解鎖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.