转自:NsstringFromName
支持系统和机型
iOS系统的指纹识别功能最低支持的机型为iPhone 5s
,最低支持系统为iOS 8
,虽然安装iOS 7
系统的5s机型可以使用系统提供的指纹解锁功能,但由于API
并未开放,所以理论上第三方软件不可使用。
依赖框架
LocalAuthentication.framework #import <LocalAuthentication/LocalAuthentication.h>
注意事项
做iOS 8
以下版本适配时,务必进行API验证,避免调用相关API引起崩溃。
使用类
LAContext
指纹验证操作对象
代码
1 - (void)authenticateUser 2 { 3 //初始化上下文对象 4 LAContext* context = [[LAContext alloc] init]; 5 //错误对象 6 NSError* error = nil; 7 NSString* result = @"Authentication is needed to access your notes."; 8 9 //首先使用canEvaluatePolicy 判断设备支持状态 10 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { 11 //支持指纹验证 12 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) { 13 if (success) { 14 //验证成功,主线程处理UI 15 } 16 else 17 { 18 NSLog(@"%@",error.localizedDescription); 19 switch (error.code) { 20 case LAErrorSystemCancel: 21 { 22 NSLog(@"Authentication was cancelled by the system"); 23 //切换到其他APP,系统取消验证Touch ID 24 break; 25 } 26 case LAErrorUserCancel: 27 { 28 NSLog(@"Authentication was cancelled by the user"); 29 //用户取消验证Touch ID 30 break; 31 } 32 case LAErrorUserFallback: 33 { 34 NSLog(@"User selected to enter custom password"); 35 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 36 //用户选择输入密码,切换主线程处理 37 }]; 38 break; 39 } 40 default: 41 { 42 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 43 //其他情况,切换主线程处理 44 }]; 45 break; 46 } 47 } 48 } 49 }]; 50 } 51 else 52 { 53 //不支持指纹识别,LOG出错误详情 54 55 switch (error.code) { 56 case LAErrorTouchIDNotEnrolled: 57 { 58 NSLog(@"TouchID is not enrolled"); 59 break; 60 } 61 case LAErrorPasscodeNotSet: 62 { 63 NSLog(@"A passcode has not been set"); 64 break; 65 } 66 default: 67 { 68 NSLog(@"TouchID not available"); 69 break; 70 } 71 } 72 73 NSLog(@"%@",error.localizedDescription); 74 } 75 }
1 typedef NS_ENUM(NSInteger, LAError) 2 { 3 //授权失败 4 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed, 5 6 //用户取消Touch ID授权 7 LAErrorUserCancel = kLAErrorUserCancel, 8 9 //用户选择输入密码 10 LAErrorUserFallback = kLAErrorUserFallback, 11 12 //系统取消授权(例如其他APP切入) 13 LAErrorSystemCancel = kLAErrorSystemCancel, 14 15 //系统未设置密码 16 LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet, 17 18 //设备Touch ID不可用,例如未打开 19 LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable, 20 21 //设备Touch ID不可用,用户未录入 22 LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled, 23 } NS_ENUM_AVAILABLE(10_10, 8_0);
操作流程
首先判断系统版本,iOS 8
及以上版本执行-(void)authenticateUser
方法,方法自动判断设备是否支持和开启Touch ID
。
iOS 9
1 /// Authentication was not successful, because there were too many failed Touch ID attempts and 2 /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating 3 /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite. 4 LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout, 5 6 /// Authentication was canceled by application (e.g. invalidate was called while 7 /// authentication was in progress). 8 LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel, 9 10 /// LAContext passed to this call has been previously invalidated. 11 LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
其中,LAErrorTouchIDLockout
是在8.0中也会出现的情况,但并未归为单独的错误类型,这个错误出现,源自用户多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁,这个错误的交互LocalAuthentication.framework
已经封装好了,不需要开发者关心。
LAErrorAppCancel
和LAErrorSystemCancel
相似,都是当前软件被挂起取消了授权,但是前者是用户不能控制的挂起,例如突然来了电话,电话应用进入前台,APP被挂起。后者是用户自己切到了别的应用,例如按home键挂起。
LAErrorInvalidContext
很好理解,就是授权过程中,LAContext对象被释放掉了,造成的授权失败
时间: 2024-10-05 14:45:36