私はビューコントローラのいくつかの行へのユーザーアクセスをブロックするアプリケーションを持っています。これは、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でそれを入れて試してみましたしかしそれはどちらもうまくいかなかった。私はどこに間違っているのか分からないようです。私はどこに間違っているのか誰にでも見える?
私は感謝しませんでした。しかし、それはまだ動作していないようです。あまりにもすべてが整然としたように見える理由はありません。 – pete800
bool変数に 'false'値を設定しています。 'viewWillAppear'か' viewDidAppear'ですか?もしそうなら、あなたの正しい値が 'false'値で上書きされています –
viewDidLoadのロックされていない変数にuserDefault値を代入します –