簡單介紹
支持系統(tǒng)和機型
iOS系統(tǒng)的指紋識別功能最低支持的機型為iPhone 5s暑脆,最低支持系統(tǒng)為iOS 8,雖然安裝iOS 7系統(tǒng)的5s機型可以使用系統(tǒng)提供的指紋解鎖功能捡鱼,但由于API并未開放,所以理論上第三方軟件不可使用。
依賴框架
LocalAuthentication.framework
import <LocalAuthentication/LocalAuthentication.h>
注意事項
做iOS 8以下版本適配時相嵌,務(wù)必進行API驗證屎暇,避免調(diào)用相關(guān)API引起崩潰承桥。
使用類
LAContext指紋驗證操作對象
代碼實現(xiàn)
//初始化上下文對象
LAContext* context = [[LAContext alloc] init]; //錯誤對象
NSError * error = nil;
NSString * result = @"驗證";
context.localizedFallbackTitle = @"123";
//判斷設(shè)備是否支持touchID
BOOL isSupport = [context canEvaluatePolicy:
LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
if (isSupport) {
//指紋識別函數(shù)
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"用 Touch ID 登錄"
reply:^(BOOL success, NSError *error) {
//如果成功
if (success) {
NSLog(@"驗證成功");
}else{
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"Authentication was cancelled by the system");
//切換到其他APP,系統(tǒng)取消驗證Touch ID
break;
}
case LAErrorUserCancel:
{
NSLog(@"Authentication was cancelled by the user");
//用戶取消驗證Touch ID
break;
}
case LAErrorUserFallback:
{
NSLog(@"User selected to enter custom password");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用戶選擇輸入密碼根悼,切換主線程處理
}];
break;
}
}
}
}];
} else {
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);
}
下面是LAError中每個枚舉對應(yīng)的含義
typedef NS_ENUM(NSInteger, LAError){
//授權(quán)失敗
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
//用戶取消Touch ID授權(quán)
LAErrorUserCancel = kLAErrorUserCancel,
//用戶選擇輸入密碼
LAErrorUserFallback = kLAErrorUserFallback,
//系統(tǒng)取消授權(quán)(例如其他APP切入)
LAErrorSystemCancel = kLAErrorSystemCancel,
//系統(tǒng)未設(shè)置密碼
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
//設(shè)備Touch ID不可用凶异,例如未打開
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
//設(shè)備Touch ID不可用蜀撑,用戶未錄入
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
}