问题描述
我的应用程序只有一个纯文本密码字段。但是,我需要用户能够在第一次之后打开应用程序,而不必每次都只在第一次时输入密码。 我不想使用钥匙串,因为密码不是由设备所有者而是其他人输入。如果需要的话,这是我的密码页面代码:
import UIKit
class LoginVC: UIViewController {
@IBOutlet weak var textpassword: UITextField!
@IBAction func btnActionLogin(_ sender: Any) {
UserDefaults.standard.set(true,forKey: "status")
if textpassword.text == "KEY" {
runbutton()
}
else {
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.present(alertController,animated: true,completion: nil)
}
}
// Create the alert controller
let alertController = UIAlertController(title: "Password",message: "Please contact Admin for access to the App",preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK",style: UIAlertAction.Style.default) {
UIAlertAction in
NSLog("OK pressed")
}
let cancelAction = UIAlertAction(title: "Cancel",style: UIAlertAction.Style.cancel) {
UIAlertAction in
NSLog("Cancel pressed")
}
func runbutton() {
let tabBarViewController = UIStoryboard(name: Constants.Storyboard.mainStoryBoard,bundle: nil).instantiateViewController(withIdentifier: Constants.Storyboard.tabBarController) as! UITabBarController
view.window?.rootViewController = tabBarViewController
view.window?.makeKeyAndVisible()
}
func touchBegin(_ touches: Set<UITouch>,with event: UIEvent?) {
view.endEditing(true)
}
struct Constants {
struct Storyboard {
static let homeViewController = "loginvc"
static let tabBarController = "TabBarVC"
static let mainStoryBoard = "Main"
}
}
}
解决方法
为什么不使用UserDefaults在其中放置密码?
How can I use UserDefaults in Swift?
我不知道您的应用或密码的安全性如何,但这可能是一种适当的方法。 写入默认值后,您可以检查密码是否写入那里,并且该应用不会再次要求输入密码。