iOS Touch ID 簡易開發教程

基礎知識

支持系統和機型

iOS系統的指紋識別功能最低支持的機型爲iPhone 5s,最低支持系統爲iOS 8,雖然安裝iOS 7系統的5s機型可使用系統提供的指紋解鎖功能,但因爲API並未開放,因此理論上第三方軟件不可以使用。segmentfault

依賴框架

LocalAuthentication.frameworkapp

#import <LocalAuthentication/LocalAuthentication.h>

注意事項

iOS 8如下版本適配時,務必進行API驗證,避免調用相關API引發崩潰。框架

使用類

LAContext 指紋驗證操做對象ui

代碼

- (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);
    }
}
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 8及以上版本執行-(void)authenticateUser方法,方法自動判斷設備是否支持和開啓Touch IDthis

iOS 9

感謝秋兒指出iOS 9加入了三種新的錯誤類型。lua

/// 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

其中,LAErrorTouchIDLockout是在8.0中也會出現的狀況,但並未歸爲單獨的錯誤類型,這個錯誤出現,源自用戶屢次連續使用Touch ID失敗,Touch ID被鎖,須要用戶輸入密碼解鎖,這個錯誤的交互LocalAuthentication.framework已經封裝好了,不須要開發者關心。線程

LAErrorAppCancelLAErrorSystemCancel類似,都是當前軟件被掛起取消了受權,可是前者是用戶不能控制的掛起,例如忽然來了電話,電話應用進入前臺,APP被掛起。後者是用戶本身切到了別的應用,例如按home鍵掛起。code

LAErrorInvalidContext很好理解,就是受權過程當中,LAContext對象被釋放掉了,形成的受權失敗。對象

相關文章
相關標籤/搜索