问题
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