標籤:
//
// ViewController.m
// TouchID指紋驗證
//
// Created by apple on 16/9/18.
// Copyright © 2016年 apple. All rights reserved.
//
#import "ViewController.h"
#import "HomeViewController.h"//跳轉成功後需要跳轉到的視圖控制器
#import <LocalAuthentication/LocalAuthentication.h>//匯入TouchID需要的類庫
#import <sys/utsname.h> //擷取裝置基本資料需要的類庫
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self touchID];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//調用TouchID會執行此方法,有需要可以實現一些另外的功能
}
- (void)touchID {
LAContext *context = [[LAContext alloc] init];
NSError *error = nil;
// 第一步判斷是否支援Touch ID 或者 本機是否已經錄入指紋
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請驗證已有指紋" reply:^(BOOL success, NSError * _Nullable error) {
if (error) {
NSLog(@"驗證失敗"); // 系統會自動給錯誤提示
}else{
dispatch_async(dispatch_get_main_queue(), ^{
// 指紋驗證成功後跳轉
HomeViewController *home = [[HomeViewController alloc] init];
[self presentViewController:home animated:YES completion:nil];
});
}
}];
}else {
// 判斷是否真機運行
if (self.isSimulator) {
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"請用真機測試~" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];
}else{
[[[UIAlertView alloc] initWithTitle:@"提示" message:@"不支援Touch ID~" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];
}
}
}
// 判斷當前裝置是否支援TouchID功能
- (BOOL)isSimulator{
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceMachine = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
if ([deviceMachine isEqualToString:@"i386"] || [deviceMachine isEqualToString:@"x86_64"]) {
return YES;
}
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS 之(個人隱私錢包調用系統TouchID指紋鎖驗證)