-3

I am having a bear of a time getting my NSUserDefaults to save when the user accepts the license. I'm guessing I am missing something simple but as I have been staring at this for two solid days now, I probably wouldn't see it if it bit me. This is my first project so please understand I am very new to this. Thank you for you patience and assistance.

In the main VC a function runs on ViewDidLoad to determine if the user has accepted the license.

override func viewDidLoad() {
    super.viewDidLoad()

    print("ViewController.swift View Did Load")

    userLicense()

Here is that function:

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

    }

If the user is not licensed, he is segued to the License Acceptance VC and this code runs:

There is a button to accept the license:

@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)
}

And a button to Decline the license:

@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)
}

As you see, it is in these button IBActions that I set the licenseStatus var to the appropriate value. The print commands show in the logs that the values are set correctly but when the user is passed back to the login VC, the value has not stuck. Here is the function where the sequel is actually called, in case this is where I am missing a step:

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

I apologize for such a lengthy post but as I said, I have been trying to implement this for two days and none of the references I can find seem to be helping (or I am just missing the forrest for the trees now that I am so far in). Thanks again for your assistance.

Greg
  • 427
  • 3
  • 7
  • 21

1 Answers1

0

You need to synchronize your NSUserDefaults after setting value to it: NSUserDefaults.standardUserDefaults().synchronize()

or in Objective-C:

[[NSUserDefaults standardUserDefaults] synchronize];

The NSUserDefaults is a file, and settings value to it is writing data to a file - a heavy IO code, so use it only if you need to save some data between app running, and synchronize it after you insert data to it. If you inseting more than one value, call it at the end, and it will flash all the last inserted data to the file.

Yedidya Reiss
  • 5,316
  • 2
  • 17
  • 19
  • Thanks for your answer. I have added the `NSUserDefaults.standardUserDefaults().synchronize()` immediately following the setting of the `NSUserDefaults` but the problem persists. As suggested in some of the comments above, I ran the debugger and my value for licenseStatus is correct when the segue takes the user back to the login screen after accepting the license, but, the value is not present in the main VC when the user gets there so the code runs as if it was the first time. HELP! – Greg Nov 17 '15 at 21:11