模仿支付宝钱包touchID功能(原创)

news/2024/7/2 2:01:13 标签: ui

最近在做个金融项目,对进入应用会有身份验证,之前用的都是手势解锁。现在用过支付宝后,发现指纹解锁挺棒的,所以想尝试换成这个功能,而且ios8也开放了这个API,所以做起来也不难。

先看下效果图。

 

1、先新建一个UIViewController吧,主要就是解锁界面搭建。

简单来说,就是进入应用时,如果需要验证,就present这个控制器,验证成功就dismiss这个控制器。

代码用了masonry框架,很优秀的autolayout框架,请自行导入。

//
//  BYUseTouchIDViewController.m//
//  Created by Sunny on 15/8/10.
//  Copyright (c) 2015年 byzk. All rights reserved.
//

#import "BYUseTouchIDViewController.h"

@interface BYUseTouchIDViewController ()

@end

@implementation BYUseTouchIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setTouchID];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self evaluatePolicy];
}

- (void)setTouchID
{
    UIImageView *contentImage = [[UIImageView alloc] init];
    contentImage.userInteractionEnabled = YES;
    contentImage.contentMode = UIViewContentModeScaleAspectFill;
    contentImage.image = [UIImage imageNamed:@"sky"];
    [self.view addSubview:contentImage];
    
    UIImage *touchIDImg = [UIImage imageNamed:@"touchID"];
    UIButton *button = [[UIButton alloc] init];
    [button setImage:touchIDImg forState:UIControlStateNormal];
    [button addTarget:self action:@selector(evaluatePolicy) forControlEvents:UIControlEventTouchUpInside];
    [contentImage addSubview:button];
    
    UIButton *touchBtn = [[UIButton alloc] init];
    touchBtn.backgroundColor = [UIColor clearColor];
    [touchBtn setTitle:@"点击进行指纹解锁" forState:UIControlStateNormal];
    [touchBtn setTitleColor:BYColor(5, 148, 223) forState:UIControlStateNormal];
    touchBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [touchBtn addTarget:self action:@selector(evaluatePolicy) forControlEvents:UIControlEventTouchUpInside];
    [contentImage addSubview:touchBtn];
    
    UIButton *loginBtn = [[UIButton alloc] init];
    loginBtn.backgroundColor = [UIColor clearColor];
    [loginBtn setTitle:@"登录其他账户" forState:UIControlStateNormal];
    [loginBtn setTitleColor:BYColor(22, 96, 140) forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [loginBtn addTarget:self action:@selector(loginOther) forControlEvents:UIControlEventTouchUpInside];
    [contentImage addSubview:loginBtn];
    
    [contentImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(contentImage);
        make.size.mas_equalTo(touchIDImg.size);
    }];
    
    [touchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentImage.mas_centerX);
        make.top.mas_equalTo(button.mas_bottom).offset(20);
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(contentImage.mas_centerX);
        make.bottom.mas_equalTo(contentImage.mas_bottom).offset(-20);
    }];
}
//进入解锁
- (void)evaluatePolicy
{
    LAContext *context = [[LAContext alloc] init];

    // show the authentication UI with our reason string
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"通过Home键验证已有手机指纹", nil) reply:
     ^(BOOL success, NSError *authenticationError) {
         if (success) {
    //解锁成功就dismiss这个控制器
             [self dismissViewControllerAnimated:NO completion:nil];
         }
     }];
}

- (void)loginOther
{
    //登陆其他账户
}

@end

 

2、然后来到AppDelegate代理

分析一下支付宝的验证模式。当用户打开应用的时候,进行验证。当应用挂入后台,再进入应用,如果这个时间差大于两分钟(我也不能明确几分钟,就当是两分钟吧)进行验证。

所以在AppDelegate.m中进行设置。

先贴出用到的几个工具类方法

//
//  BYTools.m
//
//  Created by Sunny on 15/8/4.
//  Copyright (c) 2015年 byzk. All rights reserved.
//

#import "BYTools.h"

@implementation BYTools

//这个方法用于验证touchID是否可用
+ (BOOL)canEvaluatePolicy
{
    LAContext *context = [[LAContext alloc] init];
    NSError *error;
    BOOL success;
    
    success = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    
    return success;
}
//这个方法用于获取当前时间戳
+ (long long)getNowTime
{
    UInt64 nowTime = [[NSDate date] timeIntervalSince1970];
    return nowTime;
}

@end

 

//AppDelegate.m

//这个方法用来保存时间到本地
- (void)saveTime
{
    NSString *nowTime = [NSString stringWithFormat:@"%lld", [BYTools getNowTime]];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:nowTime forKey:@"enterbgTime"];
}

/**
 进入后台
 **/
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //进入后台时,我们把这个时间保存下来。
    [self saveTime];
}

//显示touchID验证界面
- (void)showTouchID
{
    if ([BYTools canEvaluatePolicy]) {
        BYUseTouchIDViewController *useTouchID = [[BYUseTouchIDViewController alloc] init];
        [self.window.rootViewController presentViewController:useTouchID animated:NO completion:nil];
    }
}

//判断是否需要进行touchID身份验证
- (void)useTouchID
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *enterbgTime = [userDefaults objectForKey:@"enterbgTime"];
    if (enterbgTime.length != 0) {
        long long bgTime = enterbgTime.longLongValue;
        long long instanceTime = [BYTools getNowTime] - bgTime;
        if (instanceTime > 120) {
            [self saveTime];
            [self showTouchID];
        }
    }
}

//在应用被激活时调用。通常进入应用时,或者从后台进入前台时使用
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self useTouchID];
}

//在进入应用时使用验证
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //保存时间,防止重复调用
    [self saveTime];
    [self showTouchID];
    
}

 

好了,基本功能就实现,当然功能还可以继续扩展或优化。

放图片资源,从支付宝ipa里找的

sky@2x.png

 

sky@2x.png

 

touchID@2x.png

 

touchID@2x.png

转载于:https://www.cnblogs.com/c-y-q/p/4744217.html


http://www.niftyadmin.cn/n/667912.html

相关文章

java 构造方法/构造器

基本语法 [public] 方法名(形参列表){ 方法体; } 构造器的修饰符可以默认,也可以是public protected private构造器没有返回值名称 和 类名必须一样构造器的调用,由系统完成构造器是完成对象的初始化,不是创建对象在创建对象时,系…

[Android开发] Android Studio问题以及解决记录

1、真机运行报错Multi dex requires Build Tools 21.0.0 / Current: 19.1 解决: 在项目 build.gradle 里面把classpath ‘com.android.tools.build:gradle:1.5.0’ 改为1.5.0 或者1.3.0 2、导入第三方包运行报错:前言不允许有内容 解决 一般是包的位置错误&#x…

ZEOSDBO控件的安装及使用方法

步骤: 1:下载最新版的ZEOSDBO,官网:http://sourceforge.net/projects/zeoslib/ 2:解压文件到文件安装目录下:C:\Program Files\Embarcadero\RAD Studio\9.0\ZEOSDBO-7.0.4 3.运行delphi xe2,.然后从“File”菜单中选择“Open Proj…

date iso 8610

$day date_iso8601(REQUEST_TIME);dpm($day);// 2015-08-20T14:35:5608:00转载于:https://www.cnblogs.com/qinqiu/p/4745415.html

帝国 listenews.php,帝国CMS获取当前自定义列表的listid

准备用帝国自定义列表功能做几个专题,但是发现没办法调用自定义列表的当前ID(数据库字段为listid),在帝国论坛也没找到合适的答案。于是百度了一下,在这里找到了。不敢独食,马上发来和大家分享。这里需要修改底层文件functions.ph…

每辆车能坑6万元 揭开事故车翻新内幕

事故车不愁卖不出去!‘整容’过的事故车,转手便可以赚数万元!”说这话的是老卞,一位维修厂的工作人员,他每个月会给几辆准备销售的事故二手车进行全方位的“整容”。通过他,记者挖掘出了二手车中介公司和维…

Eclipse突然崩溃或电脑突然断电后,代码丢失找回

晚上遇见个奇葩的事,Eclipse正写着代码,电脑突然蓝屏,开机后Eclipse中打开的类文件中的代码丢失了,次奥,真尼玛吓尿了,不想这周的工作白费啊。 悲痛欲绝,突然右击发现了一个localhistory-->r…

buildConfigField boolean, LOG_DEBUG, true

buildConfigField "boolean", "LOG_DEBUG", "true"这个是我自定义的一个布尔值变量,它有什么用处呢?比如是程序中我可以通过BuildConfig.LOG_DEBUG 拿到这个变量,然后通过这个来书写线上环境的地址和测试环境的…