touchid指纹识别是iphone 5s设备中增加的一项重大功能.苹果的后续移动设备也相继添加了指纹功能,在实际使用中还是相当方便的,比如快捷登录,快捷支付等等.系统提供了相应框架,使用起来还是比较方便的.使用lacontext对象即可完成指纹识别,提高用户体验.
提示:指纹识别必须用真机测试,并且在ios8以上系统.
touchid api使用
1.添加头文件
#import
2.判断系统版本
1
2
3
4
5
|
//首先判断版本
if (nsfoundationversionnumber < nsfoundationversionnumber_ios_8_0) {
nslog(@ "系统版本不支持touchid" );
return ;
}
|
3.lapolicy
在这里简单介绍一下lapolicy,它是一个枚举.我们根据自己的需要选择lapolicy,它提供两个值:
lapolicydeviceownerauthenticationwithbiometrics和lapolicydeviceownerauthentication.
<1>. lapolicydeviceownerauthenticationwithbiometrics是支持ios8以上系统,使用该设备的touchid进行验证,当输入touchid验证5次失败后,touchid被锁定,只能通过锁屏后解锁设备时输入正确的解锁密码来解锁touchid。
<2>.lapolicydeviceownerauthentication是支持ios9以上系统,使用该设备的touchid或设备密码进行验证,当输入touchid验证5次失败后,touchid被锁定,会触发设备密码页面进行验证。
4. canevaluatepolicy
使用canevaluatepolicy方法判断设备是否支持touchid,返回bool为yes,该设备支持touchid。
1
|
if ([context canevaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics error:&error]) {
|
error为返回验证错误码.具体不解释了.
5. evaluatedpolicydomainstate
context.evaluatedpolicydomainstate用于判断设备上的指纹是否被更改,在lacontext被创建的时候,evaluatedpolicydomainstate才生效,可在touchid验证成功时,将它记录下来,用于下次使用touchid时校验,提高安全性。
6. evaluatepolicy
evaluatepolicy方法是对touchid进行验证,block回调中如果success为yes则验证成功,为no验证失败,并对error进行解析.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
- (ibaction)loginbuttonclick:(uibutton *)sender {
//首先判断版本
if (nsfoundationversionnumber < nsfoundationversionnumber_ios_8_0) {
nslog(@ "系统版本不支持touchid" );
return ;
}
lacontext *context = [[lacontext alloc] init];
context.localizedfallbacktitle = @ "输入密码" ;
if (@available(ios 10.0, *)) {
// context.localizedcanceltitle = @"22222";
} else {
// fallback on earlier versions
}
nserror *error = nil;
if ([context canevaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics error:&error]) {
[context evaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics localizedreason:@ "通过home键验证已有手机指纹" reply:^( bool success, nserror * _nullable error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 验证成功" );
});
} else if (error){
switch (error.code) {
case laerrorauthenticationfailed:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 验证失败" );
});
break ;
}
case laerrorusercancel:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 被用户手动取消" );
});
}
break ;
case laerroruserfallback:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "用户不使用touchid,选择手动输入密码" );
});
}
break ;
case laerrorsystemcancel:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 被系统取消 (如遇到来电,锁屏,按了home键等)" );
});
}
break ;
case laerrorpasscodenotset:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 无法启动,因为用户没有设置密码" );
});
}
break ;
case laerrortouchidnotenrolled:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 无法启动,因为用户没有设置touchid" );
});
}
break ;
case laerrortouchidnotavailable:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 无效" );
});
}
break ;
case laerrortouchidlockout:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "touchid 被锁定(连续多次验证touchid失败,系统需要用户手动输入密码)" );
});
}
break ;
case laerrorappcancel:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "当前软件被挂起并取消了授权 (如app进入了后台等)" );
});
}
break ;
case laerrorinvalidcontext:{
dispatch_async(dispatch_get_main_queue(), ^{
nslog(@ "当前软件被挂起并取消了授权 (lacontext对象无效)" );
});
}
break ;
default :
break ;
}
}
}];
} else {
nslog(@ "当前设备不支持touchid" );
}
}
|
上面这个代码, 是整个touchid的核心,也几乎是所有代码了.
验证
验证必须使用真机
结果
输入错误的时候
总结:touchid使用起来不难,重要的是使用流程逻辑.
以登录为例,一般来说流程是这样的:
- 开启指纹登录:首次登陆使用密码登录,登录后,可以设置一个开启指纹id登录的按钮,来进行指纹认证.
- 验证:检测是否支持touchid.
- 生成设备账号/密码:touchid验证通过后,根据当前已登录的账号和硬件设备token,生成设备账号/密码(规则可自定,密码要长要复杂),并保存在keychain;
- 绑定:生成设备账号/密码后,将原账号及设备账号/密码,加密后(题主使用的是rsa加密)发送到服务端进行绑定;
- 成功:验证原账号及设备账号有效后,返回相应状态,绑定成功则完成整个touchid(设备)绑定流程。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_40201300/article/details/80000495