kitchen800
kitchen800

Reputation: 227

NSUserDefaults Saving. Coding issue

I have an app that blocks User access to a few rows of a view controller. This is done by checking if a variable of type bool is set to true or false.

//variable to see if user has purchased the IAP
var unlocked: Bool = false

//formatting the cells that display the sections
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

    //blocking cells if the are not paid for.
    if unlocked == false {

        if ( indexPath.row  >= 2 ) {
            cell.userInteractionEnabled = false
            cell.contentView.alpha = 0.5
        }
        else{
            cell.userInteractionEnabled = true
            cell.contentView.alpha = 1
        }
    } else {
        //unlocking all cells once the unloced variable has been set to true.
        cell.userInteractionEnabled = true
        cell.contentView.alpha = 1
    }

    return cell
}

This works perfectly. I then have an option for the user to purchase access to the remaining rows, and hence the remaining content of the app. Once the In-app-purchase has been purchased it will run the function "updateSections()". I know this function is called upon purchase as I have tested it.

I now want to save the value of the of "unlocked" variable using NSUserDefaults. This is what I Did:

//function Called once IAP has been made, This unlocks all sections.
func unlockSections() {
    print("Thhe IAP worked")
    //This is the code for what happens once the device has bought the IAP. going to have to save what happens here in using nsuserdefaults to make sure it will work when the app opens and closes.
     unlocked = true
    tableview.reloadData()

//saving unlocked status
let defaults =  NSUserDefaults.standardUserDefaults()
defaults.setBool(unlocked, forKey: "unlockedStatus")

}

In order to read it then when the user closes and reopens the app I put this in the viewDidLoad:

//Reading saved unlocked value. ensure that purches are restored for when device is offline.
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.stringForKey("unlockedStatus")

I have also tried putting it in the viewDidAppear but that didn't work either. I can't seem to find where I am going wrong. Is anyone able to see where I am going wrong?

Upvotes: 2

Views: 87

Answers (2)

Aashish
Aashish

Reputation: 2706

You shouldn't set unlocked as a global variable for whole class, instead you can set it inside the cellForRowAtIndexPath for now. According to my understanding, the value is saved and you successfully retrieve it too. But the problem lies when; the view tries to load,

  1. UserDefaults will give it a value as it was saved before,
  2. Than, the variable inside the class executes which assign unlocked with a default value(i.e. false) as defined.

Thus, in brief:

  • Save user data (Bool value)

  • Retrieve user data

  • Check for value

In code:

//variable to see if user has purchased the IAP
var unlocked: Bool

    override func viewDidLoad() {
    super.viewDidLoad()
    //checking and assigning the value for variable after the view loads
     if defaults.boolForKey("unlockedStatus") == nil{
       unlocked = false
     }
    }

    //Save user bool data
    let defaults =  NSUserDefaults.standardUserDefaults()
    defaults.setBool(unlocked, forKey: "unlockedStatus")

    //Retrieve user bool data
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.boolForKey("unlockedStatus")

    //Code for cellForRowAtIndexPath
   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

    //blocking cells if the are not paid for.
    if unlocked == false {
       //Will get here if it is the first time saving the unlocked value, or, if only the saved value is false.
   }
}

Upvotes: 0

Harshal Valanda
Harshal Valanda

Reputation: 5451

You are saving bool value and retrieving string value. SO instead of that retrieve bool value.
Use this

let defaults = NSUserDefaults.standardUserDefaults()
defaults.boolForKey("unlockedStatus")

Hope this will help you.

Upvotes: 3

Related Questions