【Swift3】指紋解鎖

項目說明

1.初次登陸APP時,在設(shè)置完手勢密碼后彈框提示是否打開指紋解鎖
手勢密碼請參見文章:http://www.itdecent.cn/p/96c9c4824e6e
2.在設(shè)置中,通過UISwitch開關(guān)可以打開與關(guān)閉指紋解鎖
3.若打開了指紋解鎖,則每次進(jìn)入APP時可通過指紋進(jìn)入
3.APP退至后臺10S后,再次進(jìn)入需要指紋解鎖才可進(jìn)入

思路

在主控制器mainVC中添加完gestureView后,根據(jù)本地存儲,決定是否需要指紋識別
GestureView中彈窗提示是否加入指紋解鎖
            //詢問是否加入指紋驗證
                        let alertController = UIAlertController(title: "通知", message: "是否使用指紋解鎖", preferredStyle: .alert)
                        
                        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler:
                        {
                            (UIAlertAction) -> Void in
                            print("不加入指紋解鎖")
                            UserDefaults.standard.set(false, forKey: "fingerPrint")
                        })
                        let okAction = UIAlertAction(title: "確定", style: .default, handler:
                        {
                            (UIAlertAction) -> Void in
                            print("使用指紋解鎖")
                            UserDefaults.standard.set(true, forKey: "fingerPrint")
                            
                        })
                        alertController.addAction(cancelAction)
                        alertController.addAction(okAction)
                        self.viewControler().present(alertController, animated: true, completion: nil)
                        print("獲取當(dāng)前視圖的控制器\(self.viewControler())")

若已開啟指紋識別,則啟動時在mainVC中驗證指紋
//要先導(dǎo)入LocalAuthentication
import LocalAuthentication

            //指紋解鎖
            let authenticationContext = LAContext()
            var error: NSError?
            
            //步驟1:檢查Touch ID是否可用
            let isTouchIdAvailable = authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,error: &error)
            //真機上可以使用,模擬器上不可使用
            if isTouchIdAvailable
            {
                print("恭喜,Touch ID可以使用!")
                //步驟2:獲取指紋驗證結(jié)果
                authenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "需要驗證您的指紋來確認(rèn)您的身份信息", reply:
                    {
                        (success, error) -> Void in
                        if success
                        {
                            NSLog("恭喜,您通過了Touch ID指紋驗證!")
                            //回主線程去隱藏View,若是在子線程中隱藏則延遲太厲害
                            OperationQueue.main.addOperation
                                {
                                    print("當(dāng)前線程是\(Thread.current)")
                                    self.gestureView.isHidden = true
                                }
                            return
                            
                        }
                        else
                        {
                            print("抱歉,您未能通過Touch ID指紋驗證!\n\(String(describing: error))")
                        }
                })
                
            }
            else
            {
                print("指紋不能用")
            }
在設(shè)置控制器中通過UISwitch控制指紋識別的開關(guān)
        //指紋解鎖
        view.addSubview(fingerPrintSwitch)
        fingerPrintSwitch.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
        fingerPrintSwitch.center = CGPoint(x: ScreenWidth / 2, y: ScreenHeight / 2 + changeGesturesButton.height)
        let fingerPrint = UserDefaults.standard.value(forKey: "fingerPrint")
        if ((fingerPrint != nil) && fingerPrint as! Bool)
        {
            fingerPrintSwitch.isOn = true
        }
        else
        {
            fingerPrintSwitch.isOn = false
        }


    lazy var fingerPrintSwitch : UISwitch = {
        let swt = UISwitch()
        swt.onTintColor = UIColor.hexStringToColor(hexString: ColorOfBlueColor)
        swt.tintColor = UIColor.white
        swt.addTarget(self, action: #selector(fingerPrintSwitchAction(swt:)), for: UIControlEvents.valueChanged)
        return swt
    }()

    func fingerPrintSwitchAction(swt:UISwitch)
    {
        swt.isOn = !swt.isOn
        print("現(xiàn)在是打開還是關(guān)閉\(swt.isOn)")
        if swt.isOn
        {
            UserDefaults.standard.set(true, forKey: "fingerPrint")
        }
        else
        {
             UserDefaults.standard.set(false, forKey: "fingerPrint")
        }
    }
在進(jìn)入前臺的時候,如果退出時間超過10S,并且開啟了指紋解鎖,則顯示指紋解鎖,代碼同啟動時在mainVC中驗證指紋相似
注意1

在指紋解鎖中,let authenticationContext = LAContext()最好聲明為局部變量
因為在我代碼測試中發(fā)現(xiàn),當(dāng)你驗證完指紋后,30S內(nèi)再次驗證,系統(tǒng)會幫我們自動驗證,應(yīng)該是系統(tǒng)會存儲指紋的一個有效時間(大概3分鐘吧),3分鐘內(nèi)你若再次調(diào)用指紋的驗證方法,系統(tǒng)自動幫你success
若想每次都驗證,要將指紋的那個變量聲明為函數(shù)內(nèi)的局部變量,這樣每次調(diào)用的時候重新初始化,就不會有時間間隔差了

注意2

驗證完指紋后,要回主線程去隱藏View,若是在子線程中隱藏則延遲太厲害

Demo地址:
https://github.com/CarolineQian/FQFingerprintIdentification

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 好久沒寫文章了,最近也比較偷懶,今天繼續(xù)討論我實際開發(fā)中遇到的需求,那就是關(guān)于APP解鎖,大家都知道?,F(xiàn)在越來越多...
    青蛙要fly閱讀 3,252評論 2 26
  • 簡述: 在類似支付寶為首的應(yīng)用以及各種理財?shù)壬婕板X財對安全性要求較高的應(yīng)用中,目前普遍對關(guān)鍵數(shù)據(jù)都做了安全訪問限制...
    C_HPY閱讀 3,189評論 0 18
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評論 19 139
  • 知識盲點: OOP 面向?qū)ο缶幊蹋∣bject Oriented Programming,OOP) Unified...
    半紙淵閱讀 1,254評論 0 0
  • 常用模塊 fs模塊 文件讀取 異步讀取文件內(nèi)容fs.readFile("1.txt", "utf-8", func...
    big5閱讀 437評論 0 0

友情鏈接更多精彩內(nèi)容