您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 程式設計技術

iOS 指紋識別(Touch ID)程式碼實作

編輯:iOS 程式設計技術

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //1. 判斷系統版本

    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {

        //可以使用指紋識別 --> 5S 以後的機型

        //2. 判斷是否可以進行指紋識別功能

        //2.1 創建 LA 對象上下文

        LAContext *context = [[LAContext alloc] init];

        //2.2 判斷是否可以進行

        //Evaluate: 評估 Policy: 策略

        //LAPolicyDeviceOwnerAuthenticationWithBiometrics : 可以使用指紋識別技術

        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {

            // 可以使用

            //3. 開始啟用指紋識別

            //localizedReason : 顯示在界面上的原因, 一般用於提示用戶, 為什麼要使用此功能

            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請驗證指紋, 以打開高級隱藏功能" reply:^(BOOL success, NSError * _Nullable error) {

                //判斷是否成功

                if (success) {

                  //4. 指紋識別在更新 UI 時, 一定要注意, 在主線程更新

                  dispatch_sync(dispatch_get_main_queue(), ^{

                    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"這是標題" message:@"你成功了" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

                    [alert show];

                  });

                } else {

                  dispatch_sync(dispatch_get_main_queue(), ^{

                    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"這是標題" message:@"驗證失敗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

                    [alert show];

                  });

                }

                // 判斷錯誤 如果需要區分不同的錯誤來提示用戶, 必須判斷 error

                if (error) {

                    NSLog(@"error Code: %ld",error.code);

                    NSLog(@"error : %@",error.userInfo);

                }

            }];

        } else {

          NSLog(@"對不起, 5S 以上機型才能使用此功能, 請賣腎後重拾");

      }

    } else {

      NSLog(@"對不起, 系統版本過低");

    }

}

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