添加指纹识别功能

iPhone 5S开始硬件支持指纹识别功能,在iOS 8以后支持指纹识别,今天随意试了试指纹识别功能的识别,写了一个小demo

1.环境

引入头文件

#import <LocalAuthentication/LocalAuthentication.h>

  

2.创建两个页面

第一个页面添加一个button,用来触发指纹识别的功能

第二个页面就加个label//

//  ViewController.m
//  FingerPrint
//
//  Created by Silence on 16/1/22.
//  Copyright (c) 2016年 Silence. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>


@interface ViewController ()
{
    LAContext * _context;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)buttonDidClick:(id)sender {
    
    _context = [[LAContext alloc]init];
    
    BOOL isSupport = [_context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
    
    
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前系统版本不支持指纹识别" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"提示" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (!isSupport){
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前设备不支持指纹识别" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"提示" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        [_context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请按手印" reply:^(BOOL success, NSError *error) {
            if(success)
            {
          //这部分处理验证成功的逻辑 SecondViewController * se = [[SecondViewController alloc]init]; [self presentViewController:se animated:YES completion:nil];
             } }]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

  

指纹识别功能的初步集成还是比较简单的。

 

posted @ 2016-01-22 16:37  简单飞先生  阅读(180)  评论(0编辑  收藏  举报