FaceID调用的几个注意点
Posted axman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FaceID调用的几个注意点相关的知识,希望对你有一定的参考价值。
1. 要获取当前系统是否支持FaceID或TouchID,一定要在LAContext的canEvaluatePolicy条件下,否则常为空,简单说context还没有初始化。
//
// ViewController.swift
// FaceIDDemo
//
// Created by axman.wang on 2017/11/9.
// Copyright © 2017年 axman.wang. All rights reserved.
//
import UIKit
import LocalAuthentication
//AuthenticationFunctionClass
class AuthenticationWithFaceID
static let localizedReasonStr = "FaceID方式验证";
static func authenticate(complate:@escaping(Error?)->())
let context = LAContext()
var authError: NSError? = nil
if(context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError))
if context.biometryType != .typeFaceID
complate(NSError(domain:"AuthenticationWithFaceID.authenticate",code:0,userInfo:["result":"FaceID is not supported!"]))
return
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,localizedReason: localizedReasonStr,reply: (success, error) -> Void in
if success
complate(nil)
else
complate(error)
)
else
complate(authError)
class ViewController: UIViewController
@IBOutlet weak var faceidBnt: UIButton!
@IBAction func onBntPress(_ sender: UIButton)
AuthenticationWithFaceID.authenticate(error) in
if error == nil
let alert = UIAlertController(title: "提示", message: "认证成功!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
else
let alert = UIAlertController(title: "提示", message: error?.localizedDescription, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
override func viewDidLoad()
super.viewDidLoad()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
以上是关于FaceID调用的几个注意点的主要内容,如果未能解决你的问题,请参考以下文章
学习记录6-8——staticautoexternregistertypedefdefine的几个注意点