iOS TouchID 指紋識別

來源:互聯網
上載者:User

標籤:

//
//  ViewController.m
//  touchID
//
//  Created by 謝澤鋒 on 16/4/1.
//  Copyright © 2016年 xiezefeng. All rights reserved.
//

#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface ViewController ()
//驗證裝置是否支援Touch ID
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
//進行驗證的回調
- (void)evaluatePolicy:(LAPolicy)policy
       localizedReason:(NSString *)localizedReason
                 reply:(void(^)(BOOL success, NSError *error))reply;
//取消Touch Id驗證的按鈕的標題,預設標題是輸入密碼
@property (nonatomic, copy) NSString *localizedFallbackTitle;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //建立LAContext執行個體
    LAContext  *authenticationContext= [[LAContext alloc]init];
    NSError *error;
    //1:檢查Touch ID 是否可用
    if ([authenticationContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        NSLog(@"touchId 可用");
        //2:執行認證策略
        [authenticationContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"需要驗證您的指紋來確認您的身份資訊" reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                NSLog(@"通過了Touch Id指紋驗證");
            }else{
                NSLog(@"error===%@",error);
                NSLog(@"code====%ld",error.code);
                NSLog(@"errorStr ======%@",[error.userInfo objectForKey:NSLocalizedDescriptionKey]);
                if (error.code == -2) {//點擊了取消按鈕
                    NSLog(@"點擊了取消按鈕");
                }else if (error.code == -3){//點輸入密碼按鈕
                    NSLog(@"點輸入密碼按鈕");
                }else if (error.code == -1){//連續三次指紋識別錯誤
                    NSLog(@"連續三次指紋識別錯誤");
                }else if (error.code == -4){//按下電源鍵
                    NSLog(@"按下電源鍵");
                }else if (error.code == -8){//Touch ID功能被鎖定,下一次需要輸入系統密碼
                    NSLog(@"Touch ID功能被鎖定,下一次需要輸入系統密碼");
                }
                NSLog(@"未通過Touch Id指紋驗證");
            }
        }];
    }else{
        //todo goto 輸入密碼頁面
        NSLog(@"error====%@",error);
        NSLog(@"抱歉,touchId 不可用");
    }

//    LAContext *laContext = [[LAContext alloc] init];
//    NSError *error;
//    
//    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//        
//        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
//                  localizedReason:@"你允許鋒哥指紋識別嗎"
//                            reply:^(BOOL success, NSError *error) {
//                                if (success) {
//                                    NSLog(@"success to evaluate");
//                                    //do your work
//                                }
//                                if (error) {
//                                    NSLog(@"---failed to evaluate---error: %@---", error.description);
//                                    //do your error handle
//                                }
//                            }];
//    }
//    else {
//        NSLog(@"==========Not support :%@", error.description);
//        //do your error handle
//    }
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

iOS TouchID 指紋識別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.