標籤:
//
// 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 指紋識別