用户登录
用户注册

分享至

iOS Touch ID 身份认证

  • 作者: hello122846
  • 来源: 51数据库
  • 2021-09-21

ios touch id 身份认证

ios 8 及以后录了指纹的设备可以使用 touch id 进行身份认证,指纹符合录入的指纹才能认证成功。

步骤

  1. 导入 localauthentication 框架:import localauthentication
  2. 初始化 lacontext 对象:let context = lacontext()
  3. 调用 lacontext 对象的 canevaluatepolicy(_ policy: lapolicy, error: nserrorpointer) -> bool方法
  4. 上一步如果返回 false,表示不能进行认证,执行相应的失败操作;如果返回 true,调用 lacontext 对象的evaluatepolicy(_ policy: lapolicy, localizedreason: string, reply: @escaping (bool, error?) -> void)方法,在 reply 中判断是否认证成功来执行相应的操作(如果认证失败,可以获取错误码 code,看看属于 laerror.code 的哪种类型错误来执行相应的失败操作)

调用 lacontext 对象的 canevaluatepolicy 和 evaluatepolicy 方法都要传入 lapolicy 枚举类型的值,目前有两种取值:deviceownerauthenticationwithbiometrics 和 deviceownerauthentication。前一种 deviceownerauthenticationwithbiometrics 是进行指纹认证。后一种 deviceownerauthentication 是 ios 9.0 及以后才能使用,先进行指纹认证,如果指纹认证失败可以通过输入密码进行认证。

调用 lacontext 对象的 evaluatepolicy 方法会弹出指纹认证对话框。对话框会显示需要进行认证的原因(string),就是 localizedreason 参数的值。对话框有取消按钮,ios 10.0 及以后可以设置 lacontext 对象的 localizedcanceltitle 的值来改变取消按钮显示的字。如果指纹认证失败,对话框还会显示 fallback 按钮,可以设置 lacontext 对象的 localizedfallbacktitle 的值来改变 fallback 按钮显示的字。

需要注意,evaluatepolicy 方法的 reply 回调不在主线程。如果需要更新 ui 的话,要调用主线程再更新。

代码示例

代码已上传github:https://github.com/silence-github/touchiddemo

在控制器中放置一个 label 显示认证返回结果。

指纹认证代码

let context = lacontext()
context.localizedfallbacktitle = "fall back button"
if #available(ios 10.0, *) {
 context.localizedcanceltitle = "cancel button"
}
var autherror: nserror?
if context.canevaluatepolicy(.deviceownerauthenticationwithbiometrics, error: &autherror) {
 context.evaluatepolicy(.deviceownerauthenticationwithbiometrics, localizedreason: "localized reason for authentication with biometrics", reply: { (success, evaluateerror) in
 // not in main thread
 dispatchqueue.main.async {
 if success {
 self.label.text = "success"
 // do something success
 } else if let error = evaluateerror {
 self.label.text = error.localizeddescription
 // deal with error
 if let code = laerror.code(rawvalue: (error as nserror).code) {
  switch code {
  case .userfallback:
  print("fall back button clicked")
  default:
  break
  }
 }
 }
 } 
 })
} else if let error = autherror {
 label.text = error.localizeddescription
 // deal with error
}

指纹和密码认证代码

if #available(ios 9.0, *) {
 let context = lacontext()
 context.localizedfallbacktitle = "fall back button"
 if #available(ios 10.0, *) {
 context.localizedcanceltitle = "cancel button"
 }
 var autherror: nserror?
 if context.canevaluatepolicy(.deviceownerauthentication, error: &autherror) {
 context.evaluatepolicy(.deviceownerauthentication, localizedreason: "localized reason for authentication", reply: { (success, evaluateerror) in
 // not in main thread
 dispatchqueue.main.async {
 if success {
  self.label.text = "success"
  // do something success
 } else if let error = evaluateerror {
  self.label.text = error.localizeddescription
  // when fall back button clicked, user is required to enter pin. error code will not be "userfallback"
  // deal with error
 }
 }
 })
 } else if let error = autherror {
 label.text = error.localizeddescription 
 // deal with error
 }
} else {
 let alert = uialertcontroller(title: nil, message: "authentication is available on ios 9.0 or later", preferredstyle: .alert)
 alert.addaction(uialertaction(title: "ok", style: .default, handler: nil))
 present(alert, animated: true, completion: nil)
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

软件
前端设计
程序设计
Java相关