美文网首页
iOS中的指纹识别开发

iOS中的指纹识别开发

作者: mrChan1234 | 来源:发表于2017-11-10 17:35 被阅读0次

    自iPhone 5S始,苹果公司推出了全新生物安全识别技术---指纹识别验证(Touch ID)。使得我们可以更快、更轻松地对设备进行安全的访问。可贵的是,Touch ID做到了从任意角度读取指纹数据,克服了基于密码进行锁定的不便。除此之外,苹果还加入必须进行密码校验的场景,进一步确保安全。

    最重要的一点,苹果公司提供Touch ID给第三方应用程序使用,程序只会收到认证是否成功的通知,而无法访问 Touch ID 或与已注册指纹相关的数据,这一点对安全而言尤为重要。

    为了获得更高的安全性,很多银行类、支付类APP都集成了指纹、手势等二次验证功能。今天我们就利用一个demo来了解下如何利用指纹识别:

    1.首先判断机型是否支持指纹识别或者指纹识别是否可用,注意:首先导入LocalAuthentication框架及其头文件,我们可以在success回调里面做下一步的事情,比如支付宝里面的指纹支付,识别成功以后再做下一步的result界面跳转

    代码如下:

    LAContext*context = [LAContextnew];//上下文

    NSError*error;

    //判断是否支持指纹识别

    BOOLisInteligence = [contextcanEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics

    error:&error];

    if(isInteligence) {

    NSString*myLocalizedReasonString =@"请按住Home键完成验证";

    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOLsuccess, NSError *_Nullableerror) {

    if(success) {

    //在这里拿到识别结果的回调

    //do something。。。。

    }else{

    switch(error.code)

    {

    caseLAErrorAuthenticationFailed:// Authentication was not successful, because user failed to provide valid credentials

    {

    NSLog(@"授权失败");// -1 连续三次指纹识别错误

    }

    break;

    caseLAErrorUserCancel:// Authentication was canceled by user (e.g. tapped Cancel button)

    {

    NSLog(@"用户取消验证Touch ID");// -2 在TouchID对话框中点击了取消按钮

    }

    break;

    caseLAErrorUserFallback:// Authentication was canceled, because the user tapped the fallback button (Enter Password)

    {

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

    NSLog(@"用户选择输入密码,切换主线程处理");// -3 在TouchID对话框中点击了输入密码按钮

    }];

    }

    break;

    caseLAErrorSystemCancel:// Authentication was canceled by system (e.g. another application went to foreground)

    {

    NSLog(@"取消授权,如其他应用切入,用户自主");// -4 TouchID对话框被系统取消,例如按下Home或者电源键

    }

    break;

    caseLAErrorPasscodeNotSet:// Authentication could not start, because passcode is not set on the device.

    {

    NSLog(@"设备系统未设置密码");// -5

    }

    break;

    caseLAErrorTouchIDNotAvailable:// Authentication could not start, because Touch ID is not available on the device

    {

    NSLog(@"设备未设置Touch ID");// -6

    }

    break;

    caseLAErrorTouchIDNotEnrolled:// Authentication could not start, because Touch ID has no enrolled fingers

    {

    NSLog(@"用户未录入指纹");// -7

    }

    break;

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0

    caseLAErrorTouchIDLockout://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 用户连续多次进行Touch ID验证失败,Touch ID被锁,需要用户输入密码解锁,先Touch ID验证密码

    {

    NSLog(@"Touch ID被锁,需要用户输入密码解锁");// -8 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码

    }

    break;

    caseLAErrorAppCancel:// Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然来了电话,电话应用进入前台,APP被挂起啦");

    {

    NSLog(@"用户不能控制情况下APP被挂起");// -9

    }

    break;

    caseLAErrorInvalidContext:// LAContext passed to this call has been previously invalidated.

    {

    NSLog(@"LAContext传递给这个调用之前已经失效");// -10

    }

    break;

    #else

    #endif

    default:

    {

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

    NSLog(@"其他情况,切换主线程处理");

    }];

    break;

    }

    }

    }];

    }else{

    UIAlertController *alertVC= [UIAlertController alertControllerWithTitle:@"不支持指纹识别"

    message:nilpreferredStyle:UIAlertControllerStyleAlert];

    for(inti = 0; i < 1 ; i ++) {

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定"style: UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnullaction) {

    }];

    [alertVC addAction:action];

    }

    [selfpresentViewController:alertVC animated:YEScompletion:nil];

    }

    }

    相关文章

      网友评论

          本文标题:iOS中的指纹识别开发

          本文链接:https://www.haomeiwen.com/subject/mbzbmxtx.html