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.