您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 技巧大全

iOS 8 Touch ID 指紋驗證:整合 LocalAuthentication 完成身份校驗

編輯:iOS 技巧大全
[摘要]本文介紹如何在 iOS 8 應用中整合 Touch ID,透過 LocalAuthentication 框架進行指紋認證以完成使用者身份校驗,並附完整程式碼與錯誤處理說明。

在 iOS 8 中,Apple 向第三方應用開放了 Touch ID 權限的 API,讓開發者能在應用中導入指紋認證來完成使用者身份驗證。

本文主要在說明如何於應用中整合 Touch ID 來校驗使用者身份。

首先必須匯入 LocalAuthentication 框架:

#import <LocalAuthentication/LocalAuthentication.h>

要注意的是,這個庫必須搭配 Xcode 6 且連接真機才不會出現找不到的錯誤;若使用模擬器,則會提示找不到該庫。

- (IBAction)authenticateButtonTapped:(id)sender {
    LAContext *context = [[LAContext alloc] init];
    context.localizedFallbackTitle = @"輸入密碼";
    NSError *error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
              localizedReason:@"您是這設備的所有者嗎?"
                        reply:^(BOOL success, NSError *error) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                                                    message:@"你是設備主人。"
                                                                   delegate:nil
                                                          cancelButtonTitle:@"Ok"
                                                          otherButtonTitles:nil];
                    [alert show];
                });
            } else {
                /* 常見錯誤碼:
                   LAErrorAuthenticationFailed - 身份驗證失敗(3 次機會用盡)
                   LAErrorUserCancel          - 用戶點擊取消按鈕
                   LAErrorUserFallback        - 用戶點擊回退(輸入密碼)
                   LAErrorSystemCancel        - 系統取消(例如切換到其他應用)
                   LAErrorPasscodeNotSet      - 設備未設定密碼
                   LAErrorTouchIDNotAvailable - 設備不支援 Touch ID
                   LAErrorTouchIDNotEnrolled  - 尚未登記指紋 */
                switch (error.code) {
                    case LAErrorAuthenticationFailed:
                        NSLog(@"身份驗證失敗。");
                        break;
                    case LAErrorUserCancel:
                        NSLog(@"用戶點擊取消按鈕。");
                        break;
                    case LAErrorUserFallback:
                        NSLog(@"用戶點擊輸入密碼。");
                        break;
                    case LAErrorSystemCancel:
                        NSLog(@"另一個應用程式進入前景。");
                        break;
                    case LAErrorPasscodeNotSet:
                        NSLog(@"密碼在設備上沒有設定。");
                        break;
                    case LAErrorTouchIDNotAvailable:
                        NSLog(@"觸摸 ID 在設備上不可用。");
                        break;
                    case LAErrorTouchIDNotEnrolled:
                        NSLog(@"沒有登記的指紋。");
                        break;
                    default:
                        NSLog(@"Touch ID 未配置。");
                        break;
                }
            }
        }];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"錯誤提示"
                                                            message:@"您的設備沒有觸摸 ID."
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
            [alert show];
        });
    }
}

其中兩個關鍵屬性:

(1)localizedFallbackTitle:用於設定左側回退按鈕的名稱,預設為「輸入密碼」。

(2)localizedReason:用於設定提示語,說明為何要使用 Touch ID,例如程式碼中的「您是這設備的所有者嗎?」。

官方 API 設計得非常簡潔,短短幾行程式碼就能完成指紋驗證的整合。

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