在以下代碼中,它已成功實施。只需要一個修改。當應用程序最初打開管理員密碼時,應該使用默認密碼「Hello」,然後當用戶有權訪問管理員密碼時,將編輯並保存它,然後它將在密鑰「管理員密碼」帶輸入密碼的警報視圖
如何我可以在第一次使用默認密碼嗎?
func enableSectionAlert() {
let alertController = UIAlertController(title: "Admin Password", message: "Please input admin password", preferredStyle: .alert)
let enable = UIAlertAction(title: "Enable", style: .default) { (_) in
let field = alertController.textFields?[0].text
if let x = UserDefaults.standard.string(forKey: "admin password"), x == field {
self.demosSelectionEnabled()
self.showSection.isUserInteractionEnabled = false
self.showSection.textLabel?.textColor = UIColor.lightGray
}
else{
let wrongPwd = UIAlertController(title: "Wrong Admin Password", message: nil, preferredStyle:UIAlertControllerStyle.alert)
wrongPwd.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(wrongPwd, animated: true, completion: nil)
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Admin Password"
textField.isSecureTextEntry = true
}
alertController.addAction(enable)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
}
所以我只是把下面的行代碼在我的AppDelegate和多數民衆贊成它? UserDefaults.standard.set(「Hico1234」,forKey:「管理員密碼」) – habed
函數setDefaultPassword是否需要從我的enableSectionAlert中調用,或者一旦應用程序運行並且管理員密碼鍵是空的,它將接受輸入你好? – habed
我想只是從AppDelegate中的appDidFinishLaunchingWithOptions()調用它,那應該沒問題。 –