当前位置:网站首页>touchID 和 FaceID~1
touchID 和 FaceID~1
2022-07-17 00:09:00 【博BOBO】

公司项目里面要 TouchID 和 faceID 来解锁安全中心
FaceID
iPhoneX 提供的刷脸功能与之前的设备的TouchID类似,都是属于生物验证的范畴。苹果爸爸也是为了照顾开发者,这两个功能对应的API基本相同,对于之前支持TouchID的APP其实可以在不做任何修改的基础上兼容FaceID,只是在一些UI样式上需要修改。
biometryType
iOS11之后LAContext新增biometryType属性,调用时候可以根据这个属性来判断当前设备是使用FaceID还是TouchID,并据此做UI样式上的调整
typedef NS_ENUM(NSInteger, LABiometryType)
{
/// The device does not support biometry.
LABiometryTypeNone API_AVAILABLE(macos(10.13.2), ios(11.2)),
LABiometryNone API_DEPRECATED_WITH_REPLACEMENT("LABiometryTypeNone", macos(10.13, 10.13.2), ios(11.0, 11.2)) = LABiometryTypeNone,
/// The device supports Touch ID.
LABiometryTypeTouchID,
/// The device supports Face ID.
LABiometryTypeFaceID API_UNAVAILABLE(macos),
} API_AVAILABLE(macos(10.13.2), ios(11.0)) API_UNAVAILABLE(watchos, tvos);
/// Indicates the type of the biometry supported by the device.
///
/// @discussion This property is set only when canEvaluatePolicy succeeds for a biometric policy.
/// The default value is LABiometryTypeNone.
@property (nonatomic, readonly) LABiometryType biometryType API_AVAILABLE(macos(10.13.2), ios(11.0)) API_UNAVAILABLE(watchos, tvos);
2.NSFaceIDUsageDescription
使用FaceID需要在info.plist中增加NSFaceIDUsageDescription权限申请说明,这个跟定位、拍照等一样,如果不增加默认提示如下,虽然不会崩溃,但最好还是加上。
3.其他
FaceID的调用方法跟TouchID一样,都是先判断再调用,具体流程参照分割线后的TouchID部分。
4.注意
FaceID如果 不间断连续尝试 次数超过5次之后,会弹窗提示如下,同时不再执行reply:对应的block,这个需要注意

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
//!!!超出次数,提示弹窗后,这里的block不会执行!!!
}];TouchID
iPhone 5s之后苹果推出的TouchID功能绝对是登录验证的一大神器,自此之后各种APP在涉及到登录时如果不把这一方式加上,估计都不好意思说是做APP的。这就苦了我们众程序猿,在开发中免不了要遇上各种坑。
在次我将自己曾经趟过的一些坑罗列了下
调用前的判断
在调用TouchID验证弹窗前最好先判断一下设备是否支持TouchID
//创建安全验证对象
LAContext * con = [[LAContext alloc]init];
NSError * error;
//判断是否支持密码验证
/**
* LAPolicyDeviceOwnerAuthentication 可输入手机密码的验证方式
* LAPolicyDeviceOwnerAuthenticationWithBiometrics 只有指纹的验证方式
*/
BOOL can = [con canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
这里有两种验证方式可选:
. LAPolicyDeviceOwnerAuthenticationWithBiometrics iOS8.0以上支持,只有指纹验证功能
. LAPolicyDeviceOwnerAuthentication iOS 9.0以上支持,包含指纹验证与输入密码的验证方式
调用TouchID
//初始化上下文对象
LAContext *context = [[LAContext alloc] init];
//localizedFallbackTitle=@“”,不会出现“输入密码”按钮
context.localizedFallbackTitle = @"";
//错误对象
NSError *error = nil;
NSString *result = @"验证信息";
//判断是否支持密码验证
/**
*LAPolicyDeviceOwnerAuthentication 手机密码的验证方式
*LAPolicyDeviceOwnerAuthenticationWithBiometrics 指纹的验证方式
*/
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if(error.code == LAErrorTouchIDLockout) {
BOOL can = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error];
if (can) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:result reply:^(BOOL success, NSError * _Nullable error) {
}];
}
else{
NSLog(@"调起账号密码页面失败!!!");
}
}
}];
}
注意context.localizedFallbackTitle = @"";如果不设置空值,则AlertView弹窗默认会有“输入密码”的选项,但是在LAPolicyDeviceOwnerAuthenticationWithBiometrics模式下点击“输入密码”不会有反应;LAPolicyDeviceOwnerAuthentication模式下点击可以唤起输入手机密码页面,页面如下,其中除了“指纹”两字是你的app名称,其他都不可定制
后面文章会有error.code 的说明
最后,祝大家告别 Bug!!!!!!

边栏推荐
- 07-BTC-挖矿
- 一种5G空口单向时延及其可靠性的测量方法
- NFT排行榜-NFT实盘最新地址:NFT排行榜.COM
- El form special character verification
- Computed and watch, watcheffect
- 05_ Review object defineProperty
- Database connection pool and serialize to realize operations such as adding, deleting, changing and querying
- 13 K 次取反后最大化的数组和
- 软件漏洞分析入门(四)
- Namenode and secondarynamenode
猜你喜欢

Iptables and snort basic configuration

Uniapp development, upload pictures in the app and send them directly to OSS

Cento7安装mysql5.5以及升级5.7

一文揭秘育碧等传统游戏大厂的NFT策略

Recurrence of yii2 deserialization vulnerability

大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?

DHFS读写流程

数字藏品NFT“无聊猿”BAYC的内忧与外患

Redis+Caffeine两级缓存,让访问速度纵享丝滑

毒瘤 DDD
随机推荐
Introduction to software vulnerability analysis (II)
Countless times of stepping on the pit to install awvs
知名啤酒百威布局NFT,试图揭开“蓄谋已久”的上链面纱?
6 寻找比目标字母大的最小字母
Redis 高可用原理
CheckPoint and DataNode
基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
JVM 判断对象已死,实践验证GC回收
axs火爆,还有哪些打金游戏(上)
Database connection pool and serialize to realize operations such as adding, deleting, changing and querying
MapReduce环境准备
What is the QS module?
MapReduce environment preparation
Dhfs read / write process
Computed and watch, watcheffect
15 数据流的第 K 大数值
金融学 杂项记录
手把手带你从零开始完整开发经典游戏【俄罗斯方块】,全部逻辑只用不到200行代码。
雾计算中的数据安全问题综述
袋鼠云数栈基于CBO在Spark SQL优化上的探索