在以下代码中,它已成功实施。只需要一个修改。当应用程序最初打开管理员密码时,应该使用默认密码“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()调用它,那应该没问题。 –