效果圖如下
實現過程
1
|
#import <localauthentication/localauthentication.h> |
2.了解下主要的兩個方法
這個方法是判斷設備是否支持touchid的。
1
|
- ( bool )canevaluatepolicy:(lapolicy)policy error:(nserror * __autoreleasing *)error __attribute__((swift_error(none))); |
這個是用來驗證touchid的,會有彈出框出來。
1
2
3
|
- ( void )evaluatepolicy:(lapolicy)policy localizedreason:(nsstring *)localizedreason reply:( void (^)( bool success, nserror * __nullable error))reply; |
3.新建lacontext對象
主要的屬性設置
localizedfallbacktitle
:用于設置左邊的按鈕的名稱,默認是enter password.
localizedreason
:用于設置提示語,表示為什么要使用touch id
解鎖失敗界面
1
2
3
4
5
6
|
//創建lacontext lacontext *context = [lacontext new ]; //這個屬性是設置指紋輸入失敗之后的彈出框的選項 context.localizedfallbacktitle = @ "沒有忘記密碼" ; |
4.主要回調方法,包括成功以及失敗的
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
|
nserror *error = nil; if ([context canevaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics error:&error]) { nslog(@ "支持指紋識別" ); [context evaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics localizedreason:@ "指紋解鎖" reply:^( bool success, nserror * _nullable error) { if (success) { nslog(@ "驗證成功 刷新主界面" ); } else { nslog(@ "%@" ,error.localizeddescription); switch (error.code) { case laerrorsystemcancel: { nslog(@ "系統取消授權,如其他app切入" ); break ; } case laerrorusercancel: { nslog(@ "用戶取消驗證touch id" ); break ; } case laerrorauthenticationfailed: { nslog(@ "授權失敗" ); break ; } case laerrorpasscodenotset: { nslog(@ "系統未設置密碼" ); break ; } case laerrortouchidnotavailable: { nslog(@ "設備touch id不可用,例如未打開" ); break ; } case laerrortouchidnotenrolled: { nslog(@ "設備touch id不可用,用戶未錄入" ); break ; } case laerroruserfallback: { [[nsoperationqueue mainqueue] addoperationwithblock:^{ nslog(@ "用戶選擇輸入密碼,切換主線程處理" ); }]; break ; } default : { [[nsoperationqueue mainqueue] addoperationwithblock:^{ nslog(@ "其他情況,切換主線程處理" ); }]; break ; } } } }]; } else { nslog(@ "不支持指紋識別" ); switch (error.code) { case laerrortouchidnotenrolled: { nslog(@ "touchid is not enrolled" ); break ; } case laerrorpasscodenotset: { nslog(@ "a passcode has not been set" ); break ; } default : { nslog(@ "touchid not available" ); break ; } } nslog(@ "%@" ,error.localizeddescription); } |
總結
到這里指紋解鎖功能幾乎就算完成,使用確實很簡單,因為蘋果都已經給我們做好一切,對我們開發者來說就很輕松了。教程寫的很簡陋,希望大家多多包涵,如果有疑問大家可以留言交流。