【问题标题】:Remember Password Every Time? Xcode每次都记住密码? Xcode
【发布时间】:2020-09-03 18:06:51
【问题描述】:

我的应用只有纯文本密码字段。但是,我需要用户能够在第一次之后打开应用程序,而不必每次都输入密码,只有第一次。 我不想使用钥匙串,因为密码不是由设备所有者而是由其他人输入的。这是我的密码页面代码,如果这很重要:

    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"
            
        }
    }
    
}

【问题讨论】:

    标签: xcode one-time-password


    【解决方案1】:

    你为什么不使用 UserDefaults 把密码放在那里?

    How can I use UserDefaults in Swift?

    我不知道您的应用程序或密码必须有多安全,但这可能是一种合适的方式。 写入默认值后,您可以检查密码是否写在那里,并且应用程序不会再次要求输入密码。

    【讨论】:

    • 如果您完全关闭应用程序,这会起作用吗?我对开发非常陌生,需要对过去的应用程序进行一些小调整...
    • 是的。只要您不覆盖它们或从设备中删除应用程序,就会存储 UserDefaults 中的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2011-09-23
    • 2017-10-26
    • 2022-01-25
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多