IOS8 指纹解锁 Touch ID

苹果在2013年发布的新款 iPhone5s 手机支持指纹功能;


具体代码如下:

appdelegate 的界面呈现部分就不多说了;

在ViewController.m 文件中:


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

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([[self.navigationController.viewControllers firstObject] isEqual:self]) {
        self.navigationItem.title = @"ViewController1";
        LAContext *context = [LAContext new];
        NSError *error;
        context.localizedFallbackTitle = @"";// Cancel "Enter Password" option(cannot set as nil)
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"Use Touch ID to log in.", nil) reply:^(BOOL success, NSError *error) {
                    if (success) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            ViewController *vc2 = [[ViewController alloc] init];
                            [self.navigationController pushViewController:vc2 animated:YES];
                        });
                    }
                }];
        } else {
            NSLog(@"Touch ID is not available: %@", error);
        }
    } else {
        self.navigationItem.title = @"ViewController2";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

//真机测试即可




你可能感兴趣的:(ios,id,touch,指纹解锁)