iOS实现指纹识别

来源:互联网 发布:淘宝流量入口 编辑:程序博客网 时间:2024/06/08 01:14

iOS实现指纹识别

本人不经常更新博客,对于百度,谷歌一搜就能找到的基础知识一般不发表在博客上,今天分享一下指纹识别功能,每次进入进入应用都需要进行指纹识别,(适合金融类app,安全性可极大提高):

不废话,开始写怎么实现

在appdelegate的- (void)applicationWillEnterForeground:(UIApplication *)application方法中添加方法,每次应用后台切换到前台都会走这个方法,在下做了一些判断直有在开启并支持指纹识别和已有用户登陆的情况下才可以使用:

- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.    [self fingerYesOrNo];    NSLog(@"进入前台");       //通知消息的小红点点改为0    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;}-(void)fingerYesOrNo{    LAContext *myContext = [[LAContext alloc] init];    NSError *authError = nil;    BOOL fingerSwitch = [[NSUserDefaults standardUserDefaults] boolForKey:@"fingerS"];    if ((!([[NSUserDefaults standardUserDefaults] stringForKey:@"phone"] == nil) && [myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError] && fingerSwitch == YES)){        [self addWindowForfingerprint];    }}-(void)addWindowForfingerprint{    //1.创建窗口    self.window1 = [[UIWindow alloc] init];    self.window1.frame = [UIScreen mainScreen].bounds;    self.window1.backgroundColor = YSBJColor;     //2.显示窗口(成为主窗口)    [self.window1 makeKeyAndVisible];    self.touchView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];    UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];    navigationView.backgroundColor = YSBLUEColor;    [self.touchView addSubview:navigationView];    UIImageView *imageHead = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80, 100, 100)];    imageHead.centerX = self.touchView.centerX;    imageHead.layer.cornerRadius = 50;    imageHead.layer.masksToBounds = YES;    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"imageUrl"]]];    imageHead.image =  [UIImage imageWithData:data];    UIImageView *fingerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 260, 80, 80)];    fingerImage.centerX = self.touchView.centerX;    fingerImage.image = [UIImage imageNamed:@"finger"];    UILabel *textFinger = [[UILabel alloc] initWithFrame:CGRectMake(0, 340, 200, 30)];    textFinger.centerX = self.touchView.centerX;    textFinger.text = @"点击开启指纹解锁";    textFinger.textAlignment = NSTextAlignmentCenter;    textFinger.textColor = [UIColor colorWithRed:176/255.0 green:176/255.0 blue:176/255.0 alpha:1.0];    textFinger.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:17];    UIButton *fingerprint = [[UIButton alloc] initWithFrame:CGRectMake(0, 260, 100, 110)];    fingerprint.centerX = self.touchView.centerX;    fingerprint.backgroundColor = [UIColor clearColor];    [fingerprint addTarget:self action:@selector(fingerprintAction) forControlEvents:UIControlEventTouchUpInside];    UIButton *pwButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.touchView.frame.size.height-50,200, 30)];    pwButton.centerX = self.touchView.centerX;    [pwButton setTitle:@"使用密码登陆" forState:UIControlStateNormal];    [pwButton setTitleColor:[UIColor colorWithRed:22/255.0 green:143/255.0 blue:230/255.0 alpha:1.0]forState:UIControlStateNormal];    pwButton.titleLabel.font = [UIFont systemFontOfSize: 18.0];    [pwButton addTarget:self action:@selector(pwButtonAction) forControlEvents:UIControlEventTouchUpInside];    [self.touchView addSubview:fingerprint];    [self.touchView addSubview:pwButton];    [self.touchView addSubview:imageHead];    [self.touchView addSubview:fingerImage];    [self.touchView addSubview:textFinger];    [self.window1 insertSubview:self.touchView atIndex:5];    [self fingerprintIdentification];}-(void)fingerprintAction{    [self fingerprintIdentification];}-(void)pwButtonAction{    YSNavigationController *navigaC = [[YSNavigationController alloc] initWithRootViewController:[YSLoginViewController new]];    self.window.rootViewController = navigaC;    //    跳转前改回百色    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;    [self.window makeKeyAndVisible];}-(void)fingerprintIdentification{    //只有在有用户登陆的状态下才开启指纹    LAContext *myContext = [[LAContext alloc] init];    NSError *authError = nil;    NSString *myLocalizedReasonString = @"通过home键验证已有手机指纹";    // 判断设备是否支持指纹识别    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {        // 指纹识别只判断当前用户是否机主        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics                  localizedReason:myLocalizedReasonString                            reply:^(BOOL success, NSError *error) {                                if (success) {                                    // User authenticated successfully, take appropriate action                                    NSLog(@"指纹认证成功");                                    //    跳转前改回百色                                    [self.window makeKeyAndVisible];                                   // returnCode = @"1";                                    //return YES;                                } else {                                    // User did not authenticate successfully, look at error and take appropriate action                                    NSLog(@"指纹认证失败,%@",error.description);                                    // return NO;                                    // 错误码 error.code                                    // -1: 连续三次指纹识别错误                                    // -2: 在TouchID对话框中点击了取消按钮                                    // -3: 在TouchID对话框中点击了输入密码按钮                                    // -4: TouchID对话框被系统取消,例如按下Home或者电源键                                    // -8: 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码                                    //returnCode = [@(error.code) stringValue];                                }                            }];    } else {        // Could not evaluate policy; look at authError and present an appropriate message to user        NSLog(@"TouchID设备不可用");        // TouchID没有设置指纹        // 关闭密码(系统如果没有设置密码TouchID无法启用)        [self.window makeKeyAndVisible];    }}
0 0
原创粉丝点击