亲宝软件园·资讯

展开

iOS指纹验证TouchID iOS指纹验证TouchID应用学习教程

点柈 人气:0
想了解iOS指纹验证TouchID应用学习教程的相关内容吗,点柈在本文为您仔细讲解iOS指纹验证TouchID的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:iOS,指纹验证,TouchID,下面大家一起来学习吧。

指纹验证这个功能现在在一些app中经常常见,常常与数字解锁,手势解锁联合起来使用。前几天接到说实现一个指纹验证的功能,捣鼓了挺久,然后今天,我就简单的介绍下指纹验证,会做个简单的demo实现一下基本的功能。 

支持系统和机型:iOS系统的指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8。实现起来呢,其实还是很简单的,下面我们就用纯代码方式实现一个简单的demo1。

第一部分:调用原生服务实现指纹验证

这部分了解个大概就可以了

第一步:添加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;

这些方法实现结束后呢,这个功能也基本上算是完成了。因为好像篇幅太长了,看得人肯定也嫌烦,所以我准备另写一篇做一个在app被唤醒的时候启动指纹验证,分别用弹出控制器和弹出自定义view这两个方式来实现,感兴趣的话可以看下。

加载全部内容

相关教程
猜你喜欢
用户评论