iOS 关于Touch-ID的验证

我们在日常生活中经常会遇到使用软件弹出Touch-ID验证权限的问题。

例如:


Touch-ID验证.png
  • 其实很简单的只要我们引入LocalAuthentication.framework框架即可。
  • 导入框架后我们需要在项目中引入头文件
    #import <LocalAuthentication/LocalAuthentication.h>
  • 我们会发现LAContext.h中有两个方法
    - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));
    - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError * __nullable error))reply;

第一个方法官方解释,是判断设备是否支持身份验证,
policy这个参数有两个值:LAPolicyDeviceOwnerAuthentication
LAPolicyDeviceOwnerAuthenticationWithBiometrics。根据翻译大概能猜出第一个是纯粹的的身份验证,第二个参数是生物身份验证(
这个要比身份验证更安全更高级*),代码比较简单,所以就不上代码了,末尾会有GitHub 地址。

  • 这里有一个强调的点就是有人获取权限验证的时候会手机会卡屏,其实是因为验证方法不是放在主线程的,所有我们获取返回结果的时候,我们需要放到主线程进行处理,相关官方解释:

*The method does not block. Instead, the caller must provide a reply block to be
called asynchronously when evaluation finishes. The block is executed on a private
queue internal to the framework in an unspecified threading context. Other than that, no guarantee is made about which queue, thread, or run-loop the block is executed on.




GitHub下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容