一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - IOS - iOS指紋驗證TouchID應用學習教程

iOS指紋驗證TouchID應用學習教程

2021-03-01 16:49點柈 IOS

這篇文章主要為大家詳細iOS指紋驗證TouchID應用學習教程的第一篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下

指紋驗證這個功能現在在一些app中經常常見,常常與數字解鎖,手勢解鎖聯合起來使用。前幾天接到說實現一個指紋驗證的功能,搗鼓了挺久,然后今天,我就簡單的介紹下指紋驗證,會做個簡單的demo實現一下基本的功能。 

支持系統和機型:ios系統的指紋識別功能最低支持的機型為iphone 5s,最低支持系統為ios 8。實現起來呢,其實還是很簡單的,下面我們就用純代碼方式實現一個簡單的demo1。

第一部分:調用原生服務實現指紋驗證

這部分了解個大概就可以了

第一步:添加localauthentication.framework庫

iOS指紋驗證TouchID應用學習教程

iOS指紋驗證TouchID應用學習教程

iOS指紋驗證TouchID應用學習教程

第二步:在appdelegate.m中添加代碼
這個不說其實大家也都知道的吧。

?
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>

第四步:實現指紋驗證 

這一步就是很重要的地方了,在- (void)viewdidload中寫入驗證實現的代碼,這里只有兩步,因為lacontext在官方文檔中只有兩個方法:

?
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;

一個是判斷設備是否支持touchid,一個是進行驗證返回不同的結果,之前在網上經常可以一些文章中寫了,指紋驗證的第一步都是先判斷設備的系統版本等等,現在似乎都不需要了,只要調用該方法就可以了。全部的代碼 如下:

?
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 = @"需要你身份驗證呢";
 if ([context canevaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics error:&error])
 {
 [context evaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics localizedreason:result reply:^(bool success, nserror *error)
 {
 if (success)
 {
 //驗證成功,主線程處理ui
 //這個地方呢就是寫一些驗證成功之后需要做些什么事情的代碼。
 nslog(@"驗證成功");
 }
 else
 {
 //以下是一些驗證失敗的原因啥的
 nslog(@"%@",error.localizeddescription);
 switch (error.code) {
  case laerrorsystemcancel:
  {
  nslog(@"切換到其他app,系統取消驗證touch id");
  //切換到其他app,系統取消驗證touch id
  break;
  }
  case laerrorusercancel:
  {
  nslog(@"用戶取消驗證touch id");
  //用戶取消驗證touch id
  break;
  }
  case laerroruserfallback:
  {
  nslog(@"用戶選擇輸入密碼");
  [[nsoperationqueue mainqueue] addoperationwithblock:^{
  //用戶選擇其他驗證方式,切換主線程處理
  }];
  break;
  }
  default:
  {
  nslog(@"laerrorauthenticationfailed,授權失敗");
  //授權失敗
  [[nsoperationqueue mainqueue] addoperationwithblock:^{
  //其他情況,切換主線程處理
  }];
  break;
  }
 }
 }
 }];
 }else
 {
 //不支持指紋識別,log出錯誤詳情
 
 switch (error.code) {
 case laerrortouchidnotenrolled:
 {
 nslog(@"設備touch id不可用,用戶未錄入");
 break;
 }
 case laerrorpasscodenotset:
 {
 nslog(@"系統未設置密碼");
 break;
 }
 case laerrortouchidnotavailable:
 {
 nslog(@"設備touch id不可用,例如未打開");
 break;
 }
 default:
 {
 nslog(@"系統未設置密碼");
 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
//指紋驗證返回值
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);

以上呢,就是一個簡單的demo了,可能有些小問題,到時候需要的話可以自調整。這里附上這個demo的guithub鏈接看這里看這里,鏈接在這呢。

第二部分:利用現有的第三方組件實現

這個部分可以好好學習一下。

在這里呢,我要推薦一個別人寫的一個第三方的組件,就是[wjtouchid](https://github.com/hu670014125/wjtouchid);這個控件的話,在這個鏈接上其實已經有寫出怎么用了,其實不需要我再都說什么,但是我還是要說下吧。 

調用時只需要一兩行代碼調用,但是回調函數還是需要寫不少東西的。

1:復制文件進去

iOS指紋驗證TouchID應用學習教程

2:引入頭文件

#import "wjtouchid.h"

3:遵守協議

@interface viewcontroller ()<wjtouchiddelegate>

4: 創建對象

@property (nonatomic, strong) wjtouchid *touchid;

5:調用

?
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:實現回調函數

?
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驗證成功
- (void)wjtouchidauthorizesuccess;
 
 //touchid驗證失敗
- (void)wjtouchidauthorizefailure;
 
@optional
 
 //當前設備不支持指紋識別
- (void)wjtouchidisnotsupport;
 
 //當前軟件被掛起取消了授權(如突然來了電話,應用進入前臺)
- (void)wjtouchidauthorizeerrorappcancel;
 
 //取消touchid驗證 (用戶點擊了取消)
- (void)wjtouchidauthorizeerrorusercancel;
 
 //在touchid對話框中點擊輸入密碼按鈕
- (void)wjtouchidauthorizeerroruserfallback;
 
 //在驗證的touchid的過程中被系統取消 例如突然來電話、按了home鍵、鎖屏...
- (void)wjtouchidauthorizeerrorsystemcancel;
 
 //無法啟用touchid,設備沒有設置密碼
- (void)wjtouchidauthorizeerrorpasscodenotset;
 
 //多次連續使用touch id失敗,touch id被鎖,需要用戶輸入密碼解鎖
- (void)wjtouchidauthorizeerrortouchidlockout;
 
 //當前軟件被掛起取消了授權 (授權過程中,lacontext對象被釋)
- (void)wjtouchidauthorizeerrorinvalidcontext;
 
 //設備沒有錄入touchid,無法啟用touchid
- (void)wjtouchidauthorizeerrortouchidnotenrolled;
 
 //該設備的touchid無效
- (void)wjtouchidauthorizeerrortouchidnotavailable;

這些方法實現結束后呢,這個功能也基本上算是完成了。因為好像篇幅太長了,看得人肯定也嫌煩,所以我準備另寫一篇做一個在app被喚醒的時候啟動指紋驗證,分別用彈出控制器和彈出自定義view這兩個方式來實現,感興趣的話可以看下。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
  • IOSiOS開發技巧之狀態欄字體顏色的設置方法

    iOS開發技巧之狀態欄字體顏色的設置方法

    有時候我們需要根據不同的背景修改狀態欄字體的顏色,下面這篇文章主要給大家介紹了關于iOS開發技巧之狀態欄字體顏色的設置方法,文中通過示例代碼...

    夢想家-mxj8922021-05-10
  • IOSiOS中UILabel實現長按復制功能實例代碼

    iOS中UILabel實現長按復制功能實例代碼

    在iOS開發過程中,有時候會用到UILabel展示的內容,那么就設計到點擊UILabel復制它上面展示的內容的功能,也就是Label長按復制功能,下面這篇文章主要給大...

    devilx12792021-04-02
  • IOSiOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果的相關資料,需要的朋友可以參考下...

    jiangamh8882021-01-11
  • IOSiOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)

    iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和

    這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關資料,需要的朋友可以參考下...

    CodingFire13652021-02-26
  • IOSiOS實現控制屏幕常亮不變暗的方法示例

    iOS實現控制屏幕常亮不變暗的方法示例

    最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關于利用iOS如何實現控制屏幕常亮不變暗的方法,文中給出了詳細的...

    隨風13332021-04-02
  • IOS詳解iOS中多個網絡請求的同步問題總結

    詳解iOS中多個網絡請求的同步問題總結

    這篇文章主要介紹了詳解iOS中多個網絡請求的同步問題總結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    liang199111312021-03-15
  • IOSiOS中MD5加密算法的介紹和使用

    iOS中MD5加密算法的介紹和使用

    MD5加密是最常用的加密方法之一,是從一段字符串中通過相應特征生成一段32位的數字字母混合碼。對輸入信息生成唯一的128位散列值(32個字符)。這篇文...

    LYSNote5432021-02-04
  • IOSiOS開發之視圖切換

    iOS開發之視圖切換

    在iOS開發中視圖的切換是很頻繁的,獨立的視圖應用在實際開發過程中并不常見,除非你的應用足夠簡單。在iOS開發中常用的視圖切換有三種,今天我們將...

    執著丶執念5282021-01-16
主站蜘蛛池模板: 短篇小说肉| 性妲己 | 成人网18免费网 | 999久久免费高清热精品 | 狠狠色综合久久婷婷色天使 | 精品久久久噜噜噜久久7 | 国产自产一区c | 91香蕉国产在线观看免费永久 | 调教女帝 | 精品成人一区二区三区免费视频 | 国产一区二区三区欧美 | 欧美性色老妇人 | 欧美一区二区三区四区视频 | 男人和女人全黄一级毛片 | 欧亚尺码专线欧洲s码wmy | 午夜影院一区二区三区 | 日韩精品欧美激情国产一区 | 亚洲美女啪啪 | 国产精品久久久久一区二区三区 | 91午夜视频| 国产午夜精品理论片 | 欧美成人免费观看国产 | 二区三区视频 | 欧美色精品天天在线观看视频 | 久久se视频精品视频在线 | 黑人粗又长 | 亚洲国产在线播放在线 | 国产成人亚洲精品91专区高清 | 国产精品全国探花在线观看 | 女教师波多野结衣高清在线 | 久99久热只有精品国产99 | 精品免费久久久久久影院 | 男女车车好快的车车免费网站 | 亚洲国产经典 | 4hu永久地域网名入口 | 国产精品国产精品国产三级普 | 婷婷激情综合五月天 | 国产v视频 | 日本韩国一区二区三区 | 免费看又黄又爽又猛的视频软件- | 免费超级乱淫视频播放性 |