iOS Coding项目片段记录(七)

1.指纹解锁

今天项目中需要加入指纹解锁功能,之前没有了解过,只知道iOS8 新特性中有这么回事。

既然要搞,直接下载官方的demo,结合官方文档来看。

官方demo地址:https://developer.apple.com/library/content/samplecode/KeychainTouchID/Introduction/Intro.html

主要代码也就下面这些:

 1 - (void)evaluatePolicy {
 2     LAContext *context = [[LAContext alloc] init];
 3     __block  NSString *message;
 4     
 5     // Show the authentication UI with our reason string.
 6     [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Unlock access to locked feature" reply:^(BOOL success, NSError *authenticationError) {
 7          if (success) {
 8              message = @"evaluatePolicy: succes";
 9          }
10          else {
11              message = [NSString stringWithFormat:@"evaluatePolicy: %@", authenticationError.localizedDescription];
12          }
13 
14          [self printMessage:message inTextView:self.textView];
15      }];
16 }

接下来解释下 都是在干啥:

在使用过程中,需要导入的头文件是:
#import <LocalAuthentication/LocalAuthentication.h> 

接下来根据自己的实例,注释说明:

//初始化上下文对象  
    LAContext* context = [[LAContext alloc] init];  
    //错误对象  
    NSError* error = nil;  
    NSString* result = @"Authentication is needed to access your notes.";  
  
    //首先判断设备支持状态canEvaluatePolicy
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {  
        //支持指纹验证  
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {  
            if (success) {  
                //验证成功, 
            }  
            else  
            {  
                NSLog(@"%@",error.localizedDescription);  
                switch (error.code) {  
                    case LAErrorSystemCancel:  
                    {  
                        NSLog(@"Authentication was cancelled by the system");  
                        //切换到其他APP,系统取消验证Touch ID  
                        break;  
                    }  
                    case LAErrorUserCancel:  
                    {  
                        NSLog(@"Authentication was cancelled by the user");  
                        //用户取消验证Touch ID  
                        break;  
                    }  
                    case LAErrorUserFallback:  
                    {  
                        NSLog(@"User selected to enter custom password");  
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{  
                            //用户选择其他验证方式,切换主线程处理  
                        }];  
                        break;  
                    }  
                    default:  
                    {  
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{  
                           //其他情况,切换主线程处理   
                        }];  
                        break;  
                    }  
                }  
            }  
        }];  
    }  
    else  
    {  
        //对于不支持指纹识别,LOG出错误详情  
  
        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);  
        
    }  
}  


typedef NS_ENUM(NSInteger, LAError)  
{  
    //授权失败  
    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,  
  
    //用户取消Touch ID授权  
    LAErrorUserCancel           = kLAErrorUserCancel,  
  
    //用户选择输入密码  
    LAErrorUserFallback         = kLAErrorUserFallback,  
  
    //系统取消授权(例如其他APP切入)  
    LAErrorSystemCancel         = kLAErrorSystemCancel,  
  
    //系统未设置密码  
    LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,  
  
    //设备Touch ID不可用,例如未打开  
    LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,  
  
    //设备Touch ID不可用,用户未录入  
    LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,  
} NS_ENUM_AVAILABLE(10_10, 8_0);  

 

posted @ 2016-12-26 16:56  Qingyun_Qearl  阅读(275)  评论(0编辑  收藏  举报