没什么好说的,iOS8.0以后才推出的
先判断一下系统版本,在判断一下当前设备是否支持指纹解锁
LocalAuthentication.framework 有一整套的交互,指纹不行输入密码之类的,都封装好了,不用开发者去操心
- 引入依赖框架 LocalAuthentication.framework
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface ViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//iOS8.0后才支持指纹识别接口
if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
return;
}
_webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];
_webview.delegate = self;
[self.view addSubview:_webview];
[self createLa];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"开始了");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"结束了");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"失败了 %@",error);
}
- (void)createLa
{
//创建LAContext
LAContext* context = [[LAContext alloc] init];
NSError* error = nil;
NSString* result = @"请验证已有指纹";
//首先使用canEvaluatePolicy 判断设备支持状态
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指纹验证
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if (success) {
//验证成功,主线程处理UI
NSLog(@"成功啦");
//用户选择输入密码,切换主线程处理
dispatch_async(dispatch_get_main_queue(), ^{
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
NSLog(@"刷新啦");
});
}
else
{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
//系统取消授权,如其他APP切入
break;
}
case LAErrorUserCancel:
{
//用户取消验证Touch ID
break;
}
case LAErrorAuthenticationFailed:
{
//授权失败
break;
}
case LAErrorPasscodeNotSet:
{
//系统未设置密码
break;
}
case LAErrorTouchIDNotAvailable:
{
//设备Touch ID不可用,例如未打开
break;
}
case LAErrorTouchIDNotEnrolled:
{
//设备Touch ID不可用,用户未录入
break;
}
case LAErrorUserFallback:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用户选择输入密码,切换主线程处理
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情况,切换主线程处理
}];
break;
}
}
}
}];
}
else
{
//不支持指纹识别,LOG出错误详情
NSLog(@"不支持指纹识别");
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);
}
}