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