How to carry on In-App Purchase throughout whole app? (Remove ads from MULTIPLE views)

℡╲_俬逩灬. 提交于 2019-12-12 10:17:09

问题


I have written my code for an In-App Purchase to remove ads and it works flawlessly, except, it only works on the view controller where I removed my ads from. I want it to carry over throughout my whole app. How would I do that?

Better understanding :

I have Ads on a few view controllers in my app. I have a Information view controller where you can purchase the removal of ads. The thing is : it does't carry over to my other views, as if I go back to the menu view controller. Also, when I go back into the Information view, it still has the ad until I restore from purchases. I don't want the users to continuously restore purchases. How do get this to work?


回答1:


As @user523234 mentioned in the comments, you should be using a NSUserDefault to store a value that corresponds to the purchase of your IAP.

In this example we use NSUserDefaults to check if a key, removeAds, is true or false, YES or NO. The if statement will always be true until you set your key to YES. For more information on NSUserDefaults please refer to NSUserDefaults Class Reference.

// Create this globally
NSUserDefaults *defaults;

// Setup your NSUserDefaults in your viewDidLoad for example
defaults = [NSUserDefaults standardUserDefaults];

// Check If IAP Has Been Purchased
if (! [defaults boolForKey:@"removeAds"]) {
    // NOT purchased
}

// Upon the successful purchase of your IAP you set the key to YES
[defaults setBool:YES forKey:@"removeAds"];
[[NSUserDefaults standardUserDefaults] synchronize]; // Save your changes



回答2:


Daniel Storm's answer is correct. Below is the Swift equivalent:

let defaults = NSUserDefaults.standardUserDefaults()

if !defaults.boolForKey("removeAds") {
    // Not Purchased.
}

// Upon the successful purchase of your IAP, you set the key to true.
defaults.setBool(true, forKey: "removeAds")

// Save your changes.
NSUserDefaults.standardUserDefaults().synchronize()

I would recommend touching up on Objective-C syntax so you can translate things like this yourself in the future. It's fairly simple, Amit. Mark Daniel's answer as correct.



来源:https://stackoverflow.com/questions/31769235/how-to-carry-on-in-app-purchase-throughout-whole-app-remove-ads-from-multiple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!