Reputation: 2444
I just added one in-app purchase to my app. When a user click on the "purchase" button and the transaction ends, the "purchase" button is replaced by another button to access to the new features. But everytime I come back to that page or I close and open again the app, there is again the "purchase" button. How can I do to have always the new button if the user has purchased the new contents? Here is my code:
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(myaction)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"mytitle" forState:UIControlStateNormal];
button.frame = CGRectMake(156.0, 248.0, 129.0, 36.0);
[self.view addSubview:button];
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Errorr");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}
}
Upvotes: 0
Views: 1401
Reputation: 17877
You should save somewhere information about the fact that user has purchased the content.
There are a lot of ways to do that. For example, you could use NSUserDefaults
:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int version = [defaults integerForKey:@"db_version"]; // <- read int value from defaults
[defaults setValue:[NSNumber numberWithInt:2] forKey:@"db_version"]; // <- set int value
[defaults synchronize]; // <- save changes
Upvotes: 1
Reputation: 1177
You have to save this state to user prefs. Just add something like that to SKPaymentTransactionStatePurchased case:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:transaction.originalTransaction.payment.productIdentifier];
[prefs synchronize];
And after relaunching your app you have to check this setting and generate proper button.
Upvotes: 1
Reputation: 8990
You need to handle this yourself, StoreKit won't help you on that.
For example, once your purchase has been completed, save some value to the Userdefaults to remember that the purchase has already been made. In your ViewControllers viewDidLoad method, just check for the presence of that value and then let it show / hide the appropriate buttons.
Upvotes: 1