iOS指紋驗(yàn)證TouchID應(yīng)用學(xué)習(xí)教程
指紋驗(yàn)證這個(gè)功能現(xiàn)在在一些app中經(jīng)常常見,常常與數(shù)字解鎖,手勢(shì)解鎖聯(lián)合起來使用。前幾天接到說實(shí)現(xiàn)一個(gè)指紋驗(yàn)證的功能,搗鼓了挺久,然后今天,我就簡(jiǎn)單的介紹下指紋驗(yàn)證,會(huì)做個(gè)簡(jiǎn)單的demo實(shí)現(xiàn)一下基本的功能。
支持系統(tǒng)和機(jī)型:iOS系統(tǒng)的指紋識(shí)別功能最低支持的機(jī)型為iPhone 5s,最低支持系統(tǒng)為iOS 8。實(shí)現(xiàn)起來呢,其實(shí)還是很簡(jiǎn)單的,下面我們就用純代碼方式實(shí)現(xiàn)一個(gè)簡(jiǎn)單的demo1。
第一部分:調(diào)用原生服務(wù)實(shí)現(xiàn)指紋驗(yàn)證
這部分了解個(gè)大概就可以了
第一步:添加LocalAuthentication.framework庫



第二步:在appdelegate.m中添加代碼
這個(gè)不說其實(shí)大家也都知道的吧。
#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)長(zhǎng)AContext在官方文檔中只有兩個(gè)方法:
-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)用該方法就可以了。全部的代碼 如下:
- (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);
}
}
//指紋驗(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è)簡(jiǎn)單的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)建對(duì)象
@property (nonatomic, strong) WJTouchID *touchID;
5:調(diào)用
- (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ù)
@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對(duì)話框中點(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對(duì)象被釋) - (void)WJTouchIDAuthorizeErrorInvalidContext; //設(shè)備沒有錄入TouchID,無法啟用TouchID - (void)WJTouchIDAuthorizeErrorTouchIDNotEnrolled; //該設(shè)備的TouchID無效 - (void)WJTouchIDAuthorizeErrorTouchIDNotAvailable;
這些方法實(shí)現(xiàn)結(jié)束后呢,這個(gè)功能也基本上算是完成了。因?yàn)楹孟衿L(zhǎng)了,看得人肯定也嫌煩,所以我準(zhǔn)備另寫一篇做一個(gè)在app被喚醒的時(shí)候啟動(dòng)指紋驗(yàn)證,分別用彈出控制器和彈出自定義view這兩個(gè)方式來實(shí)現(xiàn),感興趣的話可以看下。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS 指紋解鎖驗(yàn)證TouchID功能
- 簡(jiǎn)單實(shí)現(xiàn)iOS指紋解鎖(TouchID)
- iOS指紋驗(yàn)證TouchID應(yīng)用學(xué)習(xí)教程2
- 淺析IOS開發(fā)TouchID指紋解鎖功能
- IOS 指紋識(shí)別詳解及實(shí)例代碼
- iOS中指紋識(shí)別常見問題匯總
- 使用Swift代碼實(shí)現(xiàn)iOS手勢(shì)解鎖、指紋解鎖實(shí)例詳解
- iOS中應(yīng)用內(nèi)添加指紋識(shí)別的實(shí)例代碼
- IOS 指紋識(shí)別兩種方式詳解及實(shí)例
- iOS指紋登錄(TouchID)集成方案詳解
相關(guān)文章
IOS應(yīng)用內(nèi)支付返回新舊Receipt適配的方法
本篇文章主要介紹了IOS應(yīng)用內(nèi)支付返回新舊Receipt適配的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
iOS實(shí)現(xiàn)只有底部邊框線的輸入框示例代碼
這篇文章給大家分享了一種利用iOS實(shí)現(xiàn)只有底部邊框線的輸入框,其實(shí)這個(gè)效果也挺常見的,本文給出了示例代碼,下面來看看如何實(shí)現(xiàn)這種效果。2016-09-09
Apple?Watch?App?Lifecycle應(yīng)用開發(fā)
這篇文章主要為大家介紹了Apple?Watch?App?Lifecycle應(yīng)用開發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
iOS+PHP注冊(cè)登錄系統(tǒng) iOS部分(下)
這篇文章主要介紹了iOS+PHP注冊(cè)登錄系統(tǒng)的iOS部分,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

