iOS指纹验证TouchID应用学习教程2

2020-01-18 19:33:55于海丽

    以上呢,我是没有判断是否支持touchid来写,是直接按照可以支持的来写的,大家在实际操作工程中还是需要加一下判断条件,因为现在还是有不支持touchid的机型的。然后就是要说一下下面这个。

[[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"touchIDISon"];

    这个值我也不知道我是用来干嘛的,好像就是告诉我你设置了指纹验证了,然后在app唤醒的时候要根据一个值来判断是否需要调用指纹验证服务,我后来想了想,好像只要

[[[NSUserDefaults standardUserDefaults]objectForKey:@"TouchID"] isEqualToString:@"1"]

    这个就可以了,但是仔细想想还是再加上一个好分辨一点,毕竟就把一个当做判断switch是否打开,一个判断唤醒的时候要不要调用的吧。在不同的回调函数里面需要写的东西还是有差别的。这样上面写完之后,就等于实现一个简单的基础页面,当然这个还不是什么大问题,因为这个很简单,下面就是要实现弹出view了。篇幅好像有点长了,我自己看的也有点烦了都。。。。。

第二部分

第一步

 创建自定义的view。在.h文件中写入方法


@interface YLSTouchidView : UIView

/**
 * 快速创建
*/
+(instancetype)touchIDView;

/**
* 弹出
*/
-(void)show;
-(void)showInView:(UIView *)view;

@end

第二步

    在.m文件中声明控件,设置页面大小,以及遵守协议,在页面出来的同时就要调用验证服务。


@interface YLSTouchidView()<WJTouchIDDelegate>

/** 指纹解锁的button */
@property (nonatomic,strong) UIButton *touchIdBtn;
/** 头像 */
@property (nonatomic,strong) UIImageView *iconView;
/** 用户名 */
@property (nonatomic,strong) UILabel *nameLabel;
/** 提示信息 */
@property (nonatomic,strong) UILabel *noticeLabel;
/** 手机号 */
@property (nonatomic,strong) NSString *phoneNumber;
/** 退出按钮 */
@property (nonatomic,strong) UIButton *quitBtn;

@property (nonatomic, strong) WJTouchID *touchID;

@end




-(instancetype)initWithFrame:(CGRect)frame
{
 self = [super initWithFrame:YLSScreenBounds];

 if (self)
 {
 self.backgroundColor = [UIColor orangeColor];

 }
 //调用指纹解锁
 WJTouchID *touchid = [[WJTouchID alloc]init];
 [touchid startWJTouchIDWithMessage:WJNotice(@"自定义信息", @"The Custom Message") fallbackTitle:WJNotice(@"", @"Fallback Title") delegate:self];
 self.touchID = touchid;
 return self;
}

第三步

设置控件的位置大小等等属性。


- (void)layoutSubviews
{
 [super layoutSubviews];
 self.iconView = [[UIImageView alloc]init];
 [self.iconView setFrame:ZLRect(128/320, 54/568, 65/320, 65/568)];
 [self.iconView setImage:[UIImage imageNamed:@"icon_myinformation"]];
 [self addSubview:self.iconView];

 self.nameLabel = [[UILabel alloc]init];
 [self.nameLabel setFrame:ZLRect(0, 125/568, 1, 28/568)];
 [self.nameLabel setText:@"151****1234"];
 [self.nameLabel setFont:[UIFont systemFontOfSize:ZCFont(15/375)]];
 [self.nameLabel setTextColor:[UIColor whiteColor]];
 [self.nameLabel setTextAlignment:NSTextAlignmentCenter];
 [self addSubview:self.nameLabel];

 self.touchIdBtn = [[UIButton alloc]init];
 [self.touchIdBtn setFrame:ZLRect(120/320, 250/568, 80/320, 80/568)];
 [self.touchIdBtn setImage:[UIImage imageNamed:@"touchImg"] forState:UIControlStateNormal];
 [self.touchIdBtn addTarget:self action:@selector(clickToCheckTouchID) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:self.touchIdBtn];

 self.noticeLabel = [[UILabel alloc]init];
 [self.noticeLabel setFrame:ZLRect(0, 339/568, 1, 22/568)];
 [self.noticeLabel setText:@"点击进行指纹解锁"];
 [self.noticeLabel setTextColor:[UIColor whiteColor]];
 [self.noticeLabel setTextAlignment:NSTextAlignmentCenter];
 [self.noticeLabel setFont:[UIFont systemFontOfSize:ZCFont(16/375)]];
 [self addSubview:self.noticeLabel];

 self.quitBtn = [[UIButton alloc]init];
 [self.quitBtn setFrame:ZLRect(0, 520/568, 1, 30/568)];
 [self.quitBtn setTitle:@"退出" forState:UIControlStateNormal];
 [self.quitBtn addTarget:self action:@selector(quitContent) forControlEvents:UIControlEventTouchUpInside];
 [self addSubview:self.quitBtn];

}