指紋驗(yàn)證這個(gè)功能現(xiàn)在在一些app中經(jīng)常常見,常常與數(shù)字解鎖,手勢解鎖聯(lián)合起來使用。前幾天接到說實(shí)現(xiàn)一個(gè)指紋驗(yàn)證的功能,搗鼓了挺久,然后今天,我就簡單的介紹下指紋驗(yàn)證,會(huì)做個(gè)簡單的demo實(shí)現(xiàn)一下基本的功能。
支持系統(tǒng)和機(jī)型:ios系統(tǒng)的指紋識(shí)別功能最低支持的機(jī)型為iphone 5s,最低支持系統(tǒng)為ios 8。實(shí)現(xiàn)起來呢,其實(shí)還是很簡單的,下面我們就用純代碼方式實(shí)現(xiàn)一個(gè)簡單的demo1。
第一部分:調(diào)用原生服務(wù)實(shí)現(xiàn)指紋驗(yàn)證
這部分了解個(gè)大概就可以了
第一步:添加localauthentication.framework庫
第二步:在appdelegate.m中添加代碼
這個(gè)不說其實(shí)大家也都知道的吧。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#import "appdelegate.h" #import "viewcontroller.h" @interface appdelegate () @end @implementation appdelegate - ( bool )application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { //appdelegate _window = [[uiwindow alloc]initwithframe:[uiscreen mainscreen].bounds]; _window.backgroundcolor = [uicolor whitecolor]; [_window makekeyandvisible]; viewcontroller *vc = [[viewcontroller alloc]init]; uinavigationcontroller *na = [[uinavigationcontroller alloc]initwithrootviewcontroller:vc]; _window.rootviewcontroller = na; return yes; } |
第三步
引入頭文件
#import <localauthentication/localauthentication.h>
第四步:實(shí)現(xiàn)指紋驗(yàn)證
這一步就是很重要的地方了,在- (void)viewdidload中寫入驗(yàn)證實(shí)現(xiàn)的代碼,這里只有兩步,因?yàn)閘acontext在官方文檔中只有兩個(gè)方法:
1
2
3
4
5
|
-canevaluatepolicy:error: //-(bool)canevaluatepolicy:(lapolicy)policy error:(nserror * __autoreleasing *)error __attribute__((swift_error(none))); -evaluatepolicy:localizedreason:reply: //- (void)evaluatepolicy:(lapolicy)policy localizedreason:(nsstring *)localizedreason reply:(void(^)(bool success, nserror * __nullable error))reply; |
一個(gè)是判斷設(shè)備是否支持touchid,一個(gè)是進(jìn)行驗(yàn)證返回不同的結(jié)果,之前在網(wǎng)上經(jīng)常可以一些文章中寫了,指紋驗(yàn)證的第一步都是先判斷設(shè)備的系統(tǒng)版本等等,現(xiàn)在似乎都不需要了,只要調(diào)用該方法就可以了。全部的代碼 如下:
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
|
- ( void )viewdidload { [super viewdidload]; self.title = @ "touchidsimpledemoone" ; lacontext *context = [[lacontext alloc]init]; nserror *error; nsstring *result = @ "需要你身份驗(yàn)證呢" ; if ([context canevaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics error:&error]) { [context evaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics localizedreason:result reply:^( bool success, nserror *error) { if (success) { //驗(yàn)證成功,主線程處理ui //這個(gè)地方呢就是寫一些驗(yàn)證成功之后需要做些什么事情的代碼。 nslog(@ "驗(yàn)證成功" ); } else { //以下是一些驗(yàn)證失敗的原因啥的 nslog(@ "%@" ,error.localizeddescription); switch (error.code) { case laerrorsystemcancel: { nslog(@ "切換到其他app,系統(tǒng)取消驗(yàn)證touch id" ); //切換到其他app,系統(tǒng)取消驗(yàn)證touch id break ; } case laerrorusercancel: { nslog(@ "用戶取消驗(yàn)證touch id" ); //用戶取消驗(yàn)證touch id break ; } case laerroruserfallback: { nslog(@ "用戶選擇輸入密碼" ); [[nsoperationqueue mainqueue] addoperationwithblock:^{ //用戶選擇其他驗(yàn)證方式,切換主線程處理 }]; break ; } default : { nslog(@ "laerrorauthenticationfailed,授權(quán)失敗" ); //授權(quán)失敗 [[nsoperationqueue mainqueue] addoperationwithblock:^{ //其他情況,切換主線程處理 }]; break ; } } } }]; } else { //不支持指紋識(shí)別,log出錯(cuò)誤詳情 switch (error.code) { case laerrortouchidnotenrolled: { nslog(@ "設(shè)備touch id不可用,用戶未錄入" ); break ; } case laerrorpasscodenotset: { nslog(@ "系統(tǒng)未設(shè)置密碼" ); break ; } case laerrortouchidnotavailable: { nslog(@ "設(shè)備touch id不可用,例如未打開" ); break ; } default : { nslog(@ "系統(tǒng)未設(shè)置密碼" ); break ; } } nslog(@ "%@" ,error.localizeddescription); } } |
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
|
//指紋驗(yàn)證返回值 typedef ns_enum(nsinteger, laerror) { /// authentication was not successful, because user failed to provide valid credentials. laerrorauthenticationfailed = klaerrorauthenticationfailed, /// authentication was canceled by user (e.g. tapped cancel button). laerrorusercancel = klaerrorusercancel, /// authentication was canceled, because the user tapped the fallback button (enter password). laerroruserfallback = klaerroruserfallback, /// authentication was canceled by system (e.g. another application went to foreground). laerrorsystemcancel = klaerrorsystemcancel, /// authentication could not start, because passcode is not set on the device. laerrorpasscodenotset = klaerrorpasscodenotset, /// authentication could not start, because touch id is not available on the device. laerrortouchidnotavailable = klaerrortouchidnotavailable, /// authentication could not start, because touch id has no enrolled fingers. laerrortouchidnotenrolled = klaerrortouchidnotenrolled, /// authentication was not successful, because there were too many failed touch id attempts and /// touch id is now locked. passcode is required to unlock touch id, e.g. evaluating /// lapolicydeviceownerauthenticationwithbiometrics will ask for passcode as a prerequisite. laerrortouchidlockout ns_enum_available(10_11, 9_0) = klaerrortouchidlockout, /// authentication was canceled by application (e.g. invalidate was called while /// authentication was in progress). laerrorappcancel ns_enum_available(10_11, 9_0) = klaerrorappcancel, /// lacontext passed to this call has been previously invalidated. laerrorinvalidcontext ns_enum_available(10_11, 9_0) = klaerrorinvalidcontext } ns_enum_available(10_10, 8_0); |
以上呢,就是一個(gè)簡單的demo了,可能有些小問題,到時(shí)候需要的話可以自調(diào)整。這里附上這個(gè)demo的guithub鏈接看這里看這里,鏈接在這呢。
第二部分:利用現(xiàn)有的第三方組件實(shí)現(xiàn)
這個(gè)部分可以好好學(xué)習(xí)一下。
在這里呢,我要推薦一個(gè)別人寫的一個(gè)第三方的組件,就是[wjtouchid](https://github.com/hu670014125/wjtouchid);這個(gè)控件的話,在這個(gè)鏈接上其實(shí)已經(jīng)有寫出怎么用了,其實(shí)不需要我再都說什么,但是我還是要說下吧。
調(diào)用時(shí)只需要一兩行代碼調(diào)用,但是回調(diào)函數(shù)還是需要寫不少東西的。
1:復(fù)制文件進(jìn)去
2:引入頭文件
#import "wjtouchid.h"
3:遵守協(xié)議
@interface viewcontroller ()<wjtouchiddelegate>
4: 創(chuàng)建對象
@property (nonatomic, strong) wjtouchid *touchid;
5:調(diào)用
1
2
3
4
5
6
7
|
- ( void )viewdidload { [super viewdidload]; //初始化 wjtouchid *touchid = [[wjtouchid alloc]init]; [touchid startwjtouchidwithmessage:wjnotice(@ "自定義信息" , @ "the custom message" ) fallbacktitle:wjnotice(@ "" , @ "fallback title" ) delegate:self]; self.touchid = touchid; } |
6:實(shí)現(xiàn)回調(diào)函數(shù)
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
|
@required //touchid驗(yàn)證成功 - ( void )wjtouchidauthorizesuccess; //touchid驗(yàn)證失敗 - ( void )wjtouchidauthorizefailure; @optional //當(dāng)前設(shè)備不支持指紋識(shí)別 - ( void )wjtouchidisnotsupport; //當(dāng)前軟件被掛起取消了授權(quán)(如突然來了電話,應(yīng)用進(jìn)入前臺(tái)) - ( void )wjtouchidauthorizeerrorappcancel; //取消touchid驗(yàn)證 (用戶點(diǎn)擊了取消) - ( void )wjtouchidauthorizeerrorusercancel; //在touchid對話框中點(diǎn)擊輸入密碼按鈕 - ( void )wjtouchidauthorizeerroruserfallback; //在驗(yàn)證的touchid的過程中被系統(tǒng)取消 例如突然來電話、按了home鍵、鎖屏... - ( void )wjtouchidauthorizeerrorsystemcancel; //無法啟用touchid,設(shè)備沒有設(shè)置密碼 - ( void )wjtouchidauthorizeerrorpasscodenotset; //多次連續(xù)使用touch id失敗,touch id被鎖,需要用戶輸入密碼解鎖 - ( void )wjtouchidauthorizeerrortouchidlockout; //當(dāng)前軟件被掛起取消了授權(quán) (授權(quán)過程中,lacontext對象被釋) - ( void )wjtouchidauthorizeerrorinvalidcontext; //設(shè)備沒有錄入touchid,無法啟用touchid - ( void )wjtouchidauthorizeerrortouchidnotenrolled; //該設(shè)備的touchid無效 - ( void )wjtouchidauthorizeerrortouchidnotavailable; |
這些方法實(shí)現(xiàn)結(jié)束后呢,這個(gè)功能也基本上算是完成了。因?yàn)楹孟衿L了,看得人肯定也嫌煩,所以我準(zhǔn)備另寫一篇做一個(gè)在app被喚醒的時(shí)候啟動(dòng)指紋驗(yàn)證,分別用彈出控制器和彈出自定義view這兩個(gè)方式來實(shí)現(xiàn),感興趣的話可以看下。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。