zoukankan      html  css  js  c++  java
  • iOS 指纹解锁

    iOS 指纹解锁,苹果已经封装好了,只需无脑撸即可.

    OC版本:

    引入头文件:

    #import <LocalAuthentication/LocalAuthentication.h>

      1 //
      2 //  ViewController.m
      3 //  TouchID
      4 //
      5 //  Created by Shaoting Zhou on 2018/1/23.
      6 //  Copyright © 2018年 Shaoting Zhou. All rights reserved.
      7 //
      8 
      9 #import "ViewController.h"
     10 #import <LocalAuthentication/LocalAuthentication.h>
     11 
     12 
     13 @interface ViewController ()
     14 
     15 @end
     16 
     17 @implementation ViewController
     18 
     19 - (void)viewDidLoad {
     20     [super viewDidLoad];
     21     // Do any additional setup after loading the view, typically from a nib.
     22     //创建LAContext
     23     LAContext *context = [LAContext new];
     24     
     25     //这个属性是设置指纹输入失败之后的弹出框的选项
     26     context.localizedFallbackTitle = @"没有忘记密码";
     27     
     28     NSError *error = nil;
     29     if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
     30         NSLog(@"支持指纹识别");
     31         [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请按home键指纹解锁" reply:^(BOOL success, NSError * _Nullable error) {
     32             if (success) {
     33                 NSLog(@"验证成功 刷新主界面");
     34             }else{
     35                 NSLog(@"%@",error.localizedDescription);
     36                 switch (error.code) {
     37                     case LAErrorSystemCancel:
     38                     {
     39                         NSLog(@"系统取消授权,如其他APP切入");
     40                         break;
     41                     }
     42                     case LAErrorUserCancel:
     43                     {
     44                         NSLog(@"用户取消验证Touch ID");
     45                         break;
     46                     }
     47                     case LAErrorAuthenticationFailed:
     48                     {
     49                         NSLog(@"授权失败");
     50                         break;
     51                     }
     52                     case LAErrorPasscodeNotSet:
     53                     {
     54                         NSLog(@"系统未设置密码");
     55                         break;
     56                     }
     57                     case LAErrorTouchIDNotAvailable:
     58                     {
     59                         NSLog(@"设备Touch ID不可用,例如未打开");
     60                         break;
     61                     }
     62                     case LAErrorTouchIDNotEnrolled:
     63                     {
     64                         NSLog(@"设备Touch ID不可用,用户未录入");
     65                         break;
     66                     }
     67                     case LAErrorUserFallback:
     68                     {
     69                         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
     70                             NSLog(@"用户选择输入密码,切换主线程处理");
     71                         }];
     72                         break;
     73                     }
     74                     default:
     75                     {
     76                         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
     77                             NSLog(@"其他情况,切换主线程处理");
     78                         }];
     79                         break;
     80                     }
     81                 }
     82             }
     83         }];
     84     }else{
     85         NSLog(@"不支持指纹识别");
     86         switch (error.code) {
     87             case LAErrorTouchIDNotEnrolled:
     88             {
     89                 NSLog(@"TouchID is not enrolled");
     90                 break;
     91             }
     92             case LAErrorPasscodeNotSet:
     93             {
     94                 NSLog(@"A passcode has not been set");
     95                 break;
     96             }
     97             default:
     98             {
     99                 NSLog(@"TouchID not available");
    100                 break;
    101             }
    102         }
    103         
    104         NSLog(@"%@",error.localizedDescription);
    105     }
    106     
    107 }
    108 
    109 
    110 
    111 - (void)didReceiveMemoryWarning {
    112     [super didReceiveMemoryWarning];
    113     // Dispose of any resources that can be recreated.
    114 }
    115 
    116 
    117 @end
    OC-指纹解锁

    Swift版本:

    引入头文件:

    import LocalAuthentication

     1 //
     2 //  ViewController.swift
     3 //  TouchID-Swift
     4 //
     5 //  Created by Shaoting Zhou on 2018/1/23.
     6 //  Copyright © 2018年 Shaoting Zhou. All rights reserved.
     7 //
     8 
     9 import UIKit
    10 import LocalAuthentication
    11 
    12 class ViewController: UIViewController {
    13     
    14     override func viewDidLoad() {
    15         super.viewDidLoad()
    16         //创建LAContext
    17         let context = LAContext.init()
    18         
    19         //这个属性是设置指纹输入失败之后的弹出框的选项
    20         context.localizedFallbackTitle  = "没有忘记密码"
    21         
    22         var error:NSError? = nil
    23         if(context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)){
    24             print("支持指纹识别")
    25             context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "请按home键指纹解锁", reply: { (success, err) in
    26                 if(success){
    27                     print("验证成功,刷新界面")
    28                 }else{
    29                     print("发生错误:(String(describing: error))")
    30                     let error:LAError = err! as! LAError
    31                     switch (error.code) {
    32                     case .systemCancel:
    33                         print("系统取消授权,如其他APP切入")
    34                         break
    35                     case .userCancel:
    36                         print("用户取消验证Touch ID")
    37                         break
    38                     case .authenticationFailed:
    39                         print("授权失败")
    40                         break
    41                     case .userFallback:
    42                         print("用户返回")
    43                         break
    44                     case .passcodeNotSet:
    45                         print("用户尚未位置指纹")
    46                         break
    47                     case .touchIDNotAvailable:
    48                         print("设备Touch ID未响应")
    49                         break
    50                     case .touchIDNotEnrolled:
    51                         print("设备Touch ID不可用")
    52                         break
    53                     case .touchIDLockout:
    54                         print("ouch ID锁住")
    55                         break
    56                     case .appCancel:
    57                         print("app验证Touch ID")
    58                         break
    59                     case .invalidContext:
    60                         print("无效Touch ID")
    61                         break
    62                     case .notInteractive:
    63                         print("不可交互")
    64                         break
    65                     }
    66                 }
    67                 
    68                 
    69             })
    70             
    71         }else{
    72             print("不支持touchID")
    73         
    74         }
    75         
    76         
    77     }
    78     
    79     
    80     
    81     
    82     
    83     override func didReceiveMemoryWarning() {
    84         super.didReceiveMemoryWarning()
    85         // Dispose of any resources that can be recreated.
    86     }
    87     
    88     
    89 }
    Swift-指纹解锁

    效果截图:

  • 相关阅读:
    【CYH-02】NOIp考砸后虐题赛:成绩:题解
    UVA12657 Boxes in a Line:题解
    洛谷团队月赛题:题解
    UVA10071 Back to High School Physics:题解
    NOIp2018普及组T3暨洛谷P5017 摆渡车:题解
    洛谷P2001 硬币的面值 题解
    洛谷P1033 自由落体 题解
    尴尬
    UVA11988 【Broken Keyboard (a.k.a. Beiju Text)】:题解
    linux基础命令学习(十二)yum命令
  • 原文地址:https://www.cnblogs.com/shaoting/p/8338381.html
Copyright © 2011-2022 走看看