2017-01-24 2 views
2

私はビューコントローラのいくつかの行へのユーザーアクセスをブロックするアプリケーションを持っています。これは、bool型の変数がtrueまたはfalseに設定されているかどうかをチェックすることによって行われます。NSUserDefaultsセーブ。コーディングの問題

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

これは完全に機能します。その後、ユーザーが残りの行、つまりアプリの残りのコンテンツへのアクセスを購入するオプションがあります。 In-app購入を購入すると、関数 "updateSections()"が実行されます。私はこの機能が購入時に呼び出されたことを私がテストしたのと同じように知っています。

NSUserDefaultsを使用して、 "unlocked"変数の値を保存します。これは私がやったことです:

//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でそれを入れて試してみましたしかしそれはどちらもうまくいかなかった。私はどこに間違っているのか分からないようです。私はどこに間違っているのか誰にでも見える?

答えて

3

ブール値と文字列値の取得を保存しています。 その代わりにbool値を取得します。
使用この

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

これはあなたを助けることを願っています。

+0

私は感謝しませんでした。しかし、それはまだ動作していないようです。あまりにもすべてが整然としたように見える理由はありません。 – pete800

+0

bool変数に 'false'値を設定しています。 'viewWillAppear'か' viewDidAppear'ですか?もしそうなら、あなたの正しい値が 'false'値で上書きされています –

+0

viewDidLoadのロックされていない変数にuserDefault値を代入します –

0

あなたは、クラス全体のグローバル変数としてロック解除を設定するべきではない代わりに、あなたは今のcellForRowAtIndexPath内に設定することができます。 私の理解によると、値は保存され、それも正常に取得されます。しかし問題はいつあるのか。ビューをロードしようと 、

  1. それは以前に保存したとして、それに値を与えるUserDefaults、
  2. よりも、クラス内の変数は、デフォルト値(すなわち偽)などでロック解除を割り当てた実行定義された。簡単にこのよう

  • 保存ユーザデータ(ブール値)

  • は、ユーザデータをコードに

  • 値をチェック

を取得します:

//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. 
    } 
} 
関連する問題