項目說明
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