iOS指紋登錄

1. 需求說明:我的頁面顯示一個指紋登錄的開關,當用戶設置指紋登錄開關開啟的之后,用戶下次登錄在登錄頁面可以使用指紋進行登錄,當開關關閉,則用戶登錄只能使用用戶名,密碼進行登錄。使用指紋登錄時需要進行校驗,當指紋不匹配,未開啟指紋或設備不支持指紋登錄,或者多次校驗失敗時,需要輸入解鎖密碼進行解鎖。

2.實現(xiàn)代碼 我的—>設置界面

// 設置是指紋登錄開關顯示并關聯(lián)指紋登錄方法

(1)cellForRow的方法中:

```

let fingerCell:FingerPrintTableViewCell?? = tableview.dequeueReusableCell(withIdentifier: fingerPrintIdentifier) as? FingerPrintTableViewCell

// 更新指紋登錄開關狀態(tài)

if let isOn = UserDefaults.standard.object(forKey: kUseTouchID) as? Bool {

fingerCell?.chooseSwitch.isOn = isOn

} else {

fingerCell?.chooseSwitch.isOn = false

}

//changeTouchID為

fingerCell?.chooseSwitch.addTarget(self, action: #selector(changeTouchID(_:)), for: .valueChanged)

fingerCell?.selectionStyle = .none

return fingerCell!

```

(2)在changeTouchID中指根據(jù)開關狀態(tài)分別調(diào)用指紋開啟于關閉的方法

@objc fileprivate func changeTouchID(_ sw: UISwitch) {

if sw.isOn {

// 打開操作

debugPrint("打開操作")

self.switchIsOn(sw: sw)

}

else

{

// 關閉操作

debugPrint("關閉操作")

self.switchIsOff(sw: sw)

}

}

(3)開啟指紋方法

//驗證指

func switchIsOn(sw: UISwitch)

{

let context = LAContext()

var message = ""

context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "通過Home鍵驗證已有的手機指紋", reply: { (success, authenticationError) in

if success == true {

// 校驗成功

message = "打開成功"

UserDefaults.standard.set(true, forKey: kUseTouchID)

UserDefaults.standard.synchronize()

DispatchQueue.main.async {

sw.isOn = true

}

} else {

// 校驗失敗

/// 取消:Canceled by user.

/// 未開啟或設備不支持:No fingers are enrolled with Touch ID.

// 多次校驗失敗? ? desc String? "Biometry is locked out." some

let desc = authenticationError?.localizedDescription

if desc?.contains("Canceled") == true {

message = ""

}else if desc?.contains("retry limit exceeded")? == true {

// 提示校驗失敗

message = "指紋不匹配"

}else if desc?.contains("Biometry is") == true {

// 多次校驗失敗,彈出用戶輸入密碼解鎖

context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "請輸入解鎖密碼", reply: { (success, error) in

if success == true {

// 密碼校驗成功

debugPrint("密碼校驗成功")

self.switchIsOn(sw: sw)

} else {

// 密碼校驗失敗

debugPrint("密碼校驗失敗")

}

})

}else {

message = "您未開啟指紋功能"

}

DispatchQueue.main.async {

sw.isOn = false

}

}

if message.characters.count > 0 {

DispatchQueue.main.async {

SVProgressHUD.showInfo(withStatus: message)

SVProgressHUD.dismiss(withDelay: 1)

}

}

debugPrint(message)

})

}

(4)關閉指紋方法

func switchIsOff(sw: UISwitch)

{

let context = LAContext()

var message = ""

context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "通過Home鍵驗證已有的手機指紋", reply: { (success, authenticationError) in

if success == true {

// 校驗成功

message = "關閉成功"

UserDefaults.standard.set(false, forKey: kUseTouchID)

UserDefaults.standard.synchronize()

DispatchQueue.main.async {

sw.isOn = false

}

} else {

// 校驗失敗

/// 取消:Canceled by user.

/// 未開啟或設備不支持:No fingers are enrolled with Touch ID.

// 多次校驗失敗? ? desc String? "Biometry is locked out." some

let desc = authenticationError?.localizedDescription

if desc?.contains("Canceled") == true {

message = ""

}else if desc?.contains("retry limit exceeded")? == true {

// 提示校驗失敗

message = "指紋不匹配"

}else if desc?.contains("Biometry is") == true {

// 多次校驗失敗,彈出用戶輸入密碼解鎖

context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "請輸入解鎖密碼", reply: { (success, error) in

if success == true {

// 密碼校驗成功

debugPrint("密碼校驗成功")

self.switchIsOff(sw: sw)

} else {

// 密碼校驗失敗

debugPrint("密碼校驗失敗")

}

})

}else {

message = "指紋關閉失敗"

}

DispatchQueue.main.async {

sw.isOn = true

}

}

if message.characters.count > 0 {

DispatchQueue.main.async {

SVProgressHUD.showInfo(withStatus: message)

SVProgressHUD.dismiss(withDelay: 1)

}

}

debugPrint(message)

})

}

到此我的界面指紋開啟與關閉可以自由切換 ,下面根據(jù)“我的”界面的設置完成登錄界面是否展示使用指紋登錄

(1)在viewWillAppear通過指紋UserDefaults取出指紋開關狀態(tài),從而設置開啟指紋登錄按鈕是否顯示

override func viewWillAppear(_ animated: Bool) {

super.viewWillAppear(animated)

if let isTouchID = UserDefaults.standard.object(forKey: kUseTouchID) as? Bool {

if isTouchID == true {

// 開啟指紋

self.startFinger()

self.fingerBtn.isHidden = false

} else {

self.fingerBtn.isHidden = true

}

} else {

self.fingerBtn.isHidden = true

}

}

(2)登錄界面開啟指紋登錄方法(原理與“我的界面相同”)

func startFinger()

{

var message = ""

let context = LAContext()

context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "通過Home鍵驗證已有的手機指紋", reply: { [weak self] (success, authenticationError) in

if success == true {

// 校驗成功

self?.autoLogin()

} else {

// 提示

// 校驗失敗

/// 取消:Canceled by user.

/// 未開啟或設備不支持:No fingers are enrolled with Touch ID.

// 三次校驗失?。篈pplication retry limit exceeded

// 多次校驗失敗:"Biometry is locked out."

// Biometry is not available in passcode lockout

let desc = authenticationError?.localizedDescription

if desc?.contains("Canceled") == true {

message = ""

} else if desc?.contains("Biometry is") == true {

// 多次校驗失敗,彈出用戶輸入密碼解鎖

context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "請輸入解鎖密碼", reply: { (success, error) in

if success == true {

// 密碼校驗成功

debugPrint("密碼校驗成功")

self?.startFinger()

} else {

// 密碼校驗失敗

debugPrint("密碼校驗失敗")

}

})

} else if desc?.contains("retry limit exceeded")? == true {

// 提示校驗失敗

message = "指紋不匹配"

}

}

if message.characters.count > 0 {

DispatchQueue.main.async {

SVProgressHUD.showInfo(withStatus: message)

SVProgressHUD.dismiss(withDelay: 1)

}

}

})

}


注意:在我的界面從本地取出開關狀態(tài)的時候swift與oc 有個明顯的區(qū)別,let isOn = UserDefaults.standard.object(forKey: kUseTouchID) as? Bool ? ? ? swift在轉(zhuǎn)換類型前得到的是一個 AnyObject此時我們理所當然轉(zhuǎn)換為bool類型從而進行判斷,而oc返回值已為bool類型,但是我們?nèi)詰胋oolvalue進行轉(zhuǎn)化,因為oc中未加boolvalue前 我們得到的bool值是通過NSUserDefaults通過key在本地的取值,如果有值,則返回yes,如果無值,則返回no,這不是開關真實的狀態(tài),所以一定要加boolvalue進行強轉(zhuǎn)!

let lineView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: lineViewHeight))

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

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

  • ios8以后,蘋果開放指紋,指紋可以用在支付,或者登錄是使用 引入頭文件 #import <LocalAuthe...
    驛路梨花處處開閱讀 1,861評論 1 2
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評論 19 139
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,391評論 0 17
  • 在分布式環(huán)境中,如何支持PC、APP(ios、android)等多端的會話共享,這也是所有公司都需要的解決方案,用...
    安琪拉_4b7e閱讀 1,842評論 2 7
  • 兩個目標: A、推銷品牌&產(chǎn)品&自己。 “您好,先生(小姐),這幾款是根據(jù)我們眾多的市場調(diào)研設計出來的原創(chuàng)新品,我...
    大海啊閱讀 555評論 0 0

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