iOS指纹验证TouchID怎么用
iOS指纹验证TouchID怎么用
这篇文章给大家分享的是有关iOS指纹验证TouchID怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
第一部分:调用原生服务实现指纹验证
这部分了解个大概就可以了
第一步:添加LocalAuthentication.framework库
第二步:在appdelegate.m中添加代码
这个不说其实大家也都知道的吧。
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //appdelegate _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; _window.backgroundColor = [UIColor whiteColor]; [_window makeKeyAndVisible]; ViewController *vc = [[ViewController alloc]init]; UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:vc]; _window.rootViewController = na; return YES; }
第三步
引入头文件
#import <LocalAuthentication/LocalAuthentication.h>
第四步:实现指纹验证
这一步就是很重要的地方了,在- (void)viewDidLoad中写入验证实现的代码,这里只有两步,因为LAContext在官方文档中只有两个方法:
-canEvaluatePolicy:error: //-(BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none))); -evaluatePolicy:localizedReason:reply: //- (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError * __nullable error))reply;
一个是判断设备是否支持touchid,一个是进行验证返回不同的结果,之前在网上经常可以一些文章中写了,指纹验证的第一步都是先判断设备的系统版本等等,现在似乎都不需要了,只要调用该方法就可以了。全部的代码 如下:
- (void)viewDidLoad { [super viewDidLoad]; self.title = @"TouchIDSimpleDemoOne"; LAContext *context = [[LAContext alloc]init]; NSError *error; NSString *result = @"需要你身份验证呢"; if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) { if (success) { //验证成功,主线程处理UI //这个地方呢就是写一些验证成功之后需要做些什么事情的代码。 NSLog(@"验证成功"); } else { //以下是一些验证失败的原因啥的 NSLog(@"%@",error.localizedDescription); switch (error.code) { case LAErrorSystemCancel: { NSLog(@"切换到其他APP,系统取消验证Touch ID"); //切换到其他APP,系统取消验证Touch ID break; } case LAErrorUserCancel: { NSLog(@"用户取消验证Touch ID"); //用户取消验证Touch ID break; } case LAErrorUserFallback: { NSLog(@"用户选择输入密码"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //用户选择其他验证方式,切换主线程处理 }]; break; } default: { NSLog(@"LAErrorAuthenticationFailed,授权失败"); //授权失败 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //其他情况,切换主线程处理 }]; break; } } } }]; }else { //不支持指纹识别,LOG出错误详情 switch (error.code) { case LAErrorTouchIDNotEnrolled: { NSLog(@"设备Touch ID不可用,用户未录入"); break; } case LAErrorPasscodeNotSet: { NSLog(@"系统未设置密码"); break; } case LAErrorTouchIDNotAvailable: { NSLog(@"设备Touch ID不可用,例如未打开"); break; } default: { NSLog(@"系统未设置密码"); break; } } NSLog(@"%@",error.localizedDescription); } }
//指纹验证返回值 typedef NS_ENUM(NSInteger, LAError) { /// Authentication was not successful, because user failed to provide valid credentials. LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed, /// Authentication was canceled by user (e.g. tapped Cancel button). LAErrorUserCancel = kLAErrorUserCancel, /// Authentication was canceled, because the user tapped the fallback button (Enter Password). LAErrorUserFallback = kLAErrorUserFallback, /// Authentication was canceled by system (e.g. another application went to foreground). LAErrorSystemCancel = kLAErrorSystemCancel, /// Authentication could not start, because passcode is not set on the device. LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet, /// Authentication could not start, because Touch ID is not available on the device. LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable, /// Authentication could not start, because Touch ID has no enrolled fingers. LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled, /// Authentication was not successful, because there were too many failed Touch ID attempts and /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite. LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout, /// Authentication was canceled by application (e.g. invalidate was called while /// authentication was in progress). LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel, /// LAContext passed to this call has been previously invalidated. LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext } NS_ENUM_AVAILABLE(10_10, 8_0);
以上呢,就是一个简单的demo了,可能有些小问题,到时候需要的话可以自调整。这里附上这个demo的guithub链接看这里看这里,链接在这呢。
第二部分:利用现有的第三方组件实现
这个部分可以好好学习一下。
在这里呢,我要推荐一个别人写的一个第三方的组件,就是[WJTouchID](https://github.com/hu670014125/WJTouchID);这个控件的话,在这个链接上其实已经有写出怎么用了,其实不需要我再都说什么,但是我还是要说下吧。
调用时只需要一两行代码调用,但是回调函数还是需要写不少东西的。
1:复制文件进去
2:引入头文件
#import "WJTouchID.h"
3:遵守协议
@interface ViewController ()<WJTouchIDDelegate>
4: 创建对象
@property (nonatomic, strong) WJTouchID *touchID;
5:调用
- (void)viewDidLoad { [super viewDidLoad]; //初始化 WJTouchID *touchid = [[WJTouchID alloc]init]; [touchid startWJTouchIDWithMessage:WJNotice(@"自定义信息", @"The Custom Message") fallbackTitle:WJNotice(@"", @"Fallback Title") delegate:self]; self.touchID = touchid; }
6:实现回调函数
@required //TouchID验证成功 - (void)WJTouchIDAuthorizeSuccess; //TouchID验证失败 - (void)WJTouchIDAuthorizeFailure; @optional //当前设备不支持指纹识别 - (void)WJTouchIDIsNotSupport; //当前软件被挂起取消了授权(如突然来了电话,应用进入前台) - (void)WJTouchIDAuthorizeErrorAppCancel; //取消TouchID验证 (用户点击了取消) - (void)WJTouchIDAuthorizeErrorUserCancel; //在TouchID对话框中点击输入密码按钮 - (void)WJTouchIDAuthorizeErrorUserFallback; //在验证的TouchID的过程中被系统取消 例如突然来电话、按了Home键、锁屏... - (void)WJTouchIDAuthorizeErrorSystemCancel; //无法启用TouchID,设备没有设置密码 - (void)WJTouchIDAuthorizeErrorPasscodeNotSet; //多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁 - (void)WJTouchIDAuthorizeErrorTouchIDLockout; //当前软件被挂起取消了授权 (授权过程中,LAContext对象被释) - (void)WJTouchIDAuthorizeErrorInvalidContext; //设备没有录入TouchID,无法启用TouchID - (void)WJTouchIDAuthorizeErrorTouchIDNotEnrolled; //该设备的TouchID无效 - (void)WJTouchIDAuthorizeErrorTouchIDNotAvailable;
感谢各位的阅读!关于“iOS指纹验证TouchID怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。
[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[