LocalAuthentication 使用说明 Swift

LocalAuthentication 是可以调用生物特殊解锁、密码解锁的库

所有相关类的关系图

iOS LocalAutherization.png

使用过程

  1. 首先要在项目 Info.plist 文件中添加 NSFaceIDUsageDescription 字段,里面的文字是输入密码时的提示信息
字段时在 Info.plist 常规模式是名字为 Privacy - Face ID Usage Description
Raw Keyes & Values 模式下名为:NSFaceIDUsageDescription
info-plist-normal.pnginfo-plist-rawvale.png
  1. 获取 LAContext
  2. 先确定你需要执行的验证方式是否可用 canEvaluatePolicy() -> Bool
  3. 执行对应的验证方式 evaluatePolicy,生物解锁(TouchID FaceID)、密码验证
  4. 处理验证结果

效果

REAULT.JPG

代码例子

//
//  AuthenticationTableVC.swift
//  CustomisedViews
//
//  Created by Kyle on 2020/3/2.
//  Copyright © 2020 KyleBing. All rights reserved.
//

import UIKit
import LocalAuthentication

class AuthenticationTableVC: UITableViewController {
    
    // 1. 获取 context
    private let context = LAContext()
    
    private var AuthenticationList = [
        (title: "Biometrics",   description: "", type: LAPolicy.deviceOwnerAuthenticationWithBiometrics),
        (title: "Password",     description: "", type: LAPolicy.deviceOwnerAuthentication)
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        context.touchIDAuthenticationAllowableReuseDuration = 10 // 10秒内重复验证,不需要再次验证
    }

    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return AuthenticationList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AuthenticationCell", for: indexPath)
        let listItem = AuthenticationList[indexPath.row]
        cell.textLabel?.text = listItem.title
        cell.detailTextLabel?.text = listItem.description
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let listItem = AuthenticationList[indexPath.row]
        var touchError: NSError?
        // 2. 查看要执行的验证方式是否可用
        if context.canEvaluatePolicy(listItem.type, error: &touchError){
            // 3. 执行验证
            context.evaluatePolicy(listItem.type,
                                   localizedReason: "Test LocalAutherization: evaluatePolicay method")
            { (success, error) in
                // 4.1 success
                if success {
                    DispatchQueue.main.async {
                        self.AuthenticationList[indexPath.row].description = "success"
                        self.navigationItem.rightBarButtonItem = UIBarButtonItem(
                            title: String(format: "%.f", self.context.touchIDAuthenticationAllowableReuseDuration),
                            style: .plain,
                            target: nil,
                            action: nil)
                    }
                    
                // 4.2 error occured
                } else if let error = error {
                    DispatchQueue.main.async {
                        self.AuthenticationList[indexPath.row].description = error.localizedDescription
                    }
                    print(error.localizedDescription)
                }
                DispatchQueue.main.async {
                    self.tableView.reloadRows(at: [indexPath], with: .right)
                }
            }
        }
    }
}

KyleBing
659 声望18 粉丝

前端,喜欢 Javascript scss,喜欢做一些实用的小工具