LocalAuthentication 使用說明 Swift iOS

LocalAuthentication 使用說明 Swift

LocalAuthentication 是能夠調用生物特殊解鎖、密碼解鎖的庫swift

全部相關類的關係圖

iOS LocalAutherization.png

使用過程

  1. 首先要在項目 Info.plist 文件中添加 NSFaceIDUsageDescription 字段,裏面的文字是輸入密碼時的提示信息
字段時在 Info.plist 常規模式是名字爲 Privacy - Face ID Usage Description
Raw Keyes & Values 模式下名爲: NSFaceIDUsageDescription
info-plist-normal.png info-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)
                }
            }
        }
    }
}
相關文章
相關標籤/搜索