NSUserDefaults Saving。编码问题

时间:2017-01-24 12:04:03

标签: ios swift swift2 save nsuserdefaults

我有一个应用程序阻止用户访问视图控制器的几行。这是通过检查bool类型的变量是否设置为true或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
}

这完美无缺。然后,我可以选择让用户购买剩余行的访问权限,从而购买应用程序的剩余内容。一旦购买了应用内购买,它将运行功能" updateSections()"。我知道这个功能是在我购买时调用的。

我现在想要保存"解锁"的价值。变量使用NSUserDefaults。这就是我的所作所为:

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

}

为了阅读它,当用户关闭并重新打开应用程序时,我把它放在viewDidLoad中:

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

我也尝试将它放在viewDidAppear中,但那也没有用。我似乎无法找到我错的地方。有人能够看到我出错的地方吗?

2 个答案:

答案 0 :(得分:3)

您正在保存bool值并检索字符串值。 因此,而不是检索bool值 使用此

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

希望这会对你有所帮助。

答案 1 :(得分:0)

您不应将解锁设置为整个班级的全局变量,而是暂时将其设置在cellForRowAtIndexPath内。 根据我的理解,该值已保存,您也可以成功检索它。但问题在于;  视图尝试加载,

  1. UserDefaults将为其提供之前保存的值,
  2. 然后,类中的变量执行,将解锁分配为定义的默认值(即false)。
  3. 因此,简而言之:

    • 保存用户数据(Bool值)

    • 检索用户数据

    • 检查值

    在代码中:

    //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.
       }
    }