NSUserDefaults未保存

时间:2015-11-17 19:06:39

标签: ios swift2 nsuserdefaults

当用户接受许可时,我有一段时间让NSUserDefaults保存。我猜我错过了一些简单的东西但是因为我现在已经盯着这两天了,如果它咬了我的话,我可能不会看到它。这是我的第一个项目,所以请理解我对此很新。谢谢你的耐心和帮助。

在主VC中,一个函数在ViewDidLoad上运行,以确定用户是否已接受许可。

override func viewDidLoad() {
    super.viewDidLoad()

    print("ViewController.swift View Did Load")

    userLicense()

这是该功能:

func userLicense() {

    if licenseStatus == nil {

        print("nil status - The NSUserDefaults value for lisenceStatus is", licenseStatus)
        segueIdentifier = "showEULA"
        self.segue()
        print("First Run or Not yet accepted or declined. License Status var = ",licenseStatus)

    } else {

        licenseStatus = NSUserDefaults.standardUserDefaults().objectForKey("licenseStatus")! as? String
        print("userLicense func licenseStatus var is ",licenseStatus)

        if licenseStatus == "Declined License" {
            print("declined status - The NSUserDefaults value for lisenceStatus is", licenseStatus)
            segueIdentifier = "showEULA"
            segue()
            print("Declined License. Send user back to EULA screen")
        } else
            if licenseStatus == "Accepted License" {
                NSUserDefaults.standardUserDefaults().objectForKey("licenseStatus")
                print("accepted status - The NSUserDefaults value for lisenceStatus is", licenseStatus,". The app should run.")
                //segueIdentifier = "backToLoginScreen"
                //segue()
                //print("Accepted License")
        }
    }

    }

如果用户未获得许可,则他将被许可接受VC和此代码运行:

有一个接受许可证的按钮:

@IBAction func acceptEULA(sender: AnyObject) {
    NSUserDefaults.standardUserDefaults().setObject("Accepted License", forKey: "licenseStatus")
    licenseStatus = NSUserDefaults.standardUserDefaults().objectForKey("licenseStatus")! as! String
    print("The Accepted func NSUserDefaults license Status is ", licenseStatus)
    segueIdentifier = "backToLoginScreen"
    self.passThroughError = "You have chosen to accept the user license. Click cancel to return, otherwise enjoy using MORE 2 Go."
    self.passThroughErrorAlertController("Confirm Accept License", error: passThroughError)
}

一个拒绝许可证的按钮:

@IBAction func cancelApp(sender: AnyObject) {
    NSUserDefaults.standardUserDefaults().setObject("Declined License", forKey: "licenseStatus")
    licenseStatus = (NSUserDefaults.standardUserDefaults().objectForKey("licenseStatus")! as? String)!
    print("The NSUserDefaults licenseStatus is ", self.licenseStatus)
    segueIdentifier = "backToLoginScreen"
    self.passThroughError = "You have chosen to decine the user license. Click cancel to return, otherwise click OK and the app will be inactive."
    self.passThroughErrorAlertController("Confirm Decline License", error: passThroughError)
}

如您所见,我在IBActions按钮中将licenseStatus var设置为适当的值。 print命令在日志中显示值已正确设置但是当用户传递回登录VC时,该值未卡住。这是实际调用续集的函数,如果这是我错过了一步:

func passThroughErrorAlertController(title:String, error:String) {
    let passThroughAlert = UIAlertController(title: title, message: passThroughError, preferredStyle: UIAlertControllerStyle.Alert)
    passThroughAlert.addAction((UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)))
    passThroughAlert.addAction((UIAlertAction(title: "OK", style: .Default, handler: {action in
        print("The user clisked OK, the license status is ", self.licenseStatus)
        self.performSegueWithIdentifier(self.segueIdentifier, sender: self)
        //self.dismissViewControllerAnimated(true, completion: nil)
    })))
    self.presentViewController(passThroughAlert, animated: true, completion: nil)
}

我为这么长的帖子道歉,但正如我所说,我一直试图实施这两天,我找不到任何参考资料似乎都在帮助(或者我现在只是错过了树木的福雷斯特我到目前为止。)再次感谢您的协助。

1 个答案:

答案 0 :(得分:0)

设置值后,您需要synchronize NSUserDefaultsNSUserDefaults.standardUserDefaults().synchronize()

Objective-C

[[NSUserDefaults standardUserDefaults] synchronize];

NSUserDefaults是一个文件,其设置值是将数据写入文件 - 一个繁重的IO代码,所以只有在需要在运行的应用程序和{{1}之间保存一些数据时才使用它你向它插入数据后它。 如果您输入多个值,请在最后调用它,它会将所有最后插入的数据闪存到文件中。