Pretty long question but easy to understand so please take a minute 🙂
A few weeks ago I created a shop page for my app. I was new to the in-app purchases and got some code of the net. This was made for 1 object so I decided to edit the code to my own needs. one purchase for the pro version of the app and 3 purchases for 3 different amount of coins.
When I buy coins it works fine the only problem is that the second time I buy it will add 2x the amount of coins I bought, the third time it will add 3x the amount of coins I bought etc. I hope someone can take a look at my code and tell me whats wrong. For this topic I’ve made the code a little smaller and will not show all purchases.
//the 100 coins button
-(IBAction)savedata100:(id)sender {
askToPurchase100Munten = [[UIAlertView alloc]
initWithTitle:@"100 Munten"
message:@"Ga verder om 100 Munten te kopen."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Verder", @"Cancel", nil];
askToPurchase100Munten.delegate = self;
[askToPurchase100Munten show];
[askToPurchase100Munten release];
//the next step
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
else if (alertView==askToPurchase100Munten) {
if (buttonIndex==0) {
// user tapped YES, but we need to check if IAP is enabled or not.
if ([SKPaymentQueue canMakePayments]) {
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"100Munten"]];
Temp = 2;
request.delegate = self;
[request start];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Prohibited"
message:@"Parental Control is enabled, cannot make a purchase!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
[tmp release];
}
}
}
//the next step
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse: (SKProductsResponse *)response
{
statusLabel.text = @"";
int count = [response.products count];
}
if (Temp == 2){
if (count>0) {
SKProduct *selectedProduct = [response.products objectAtIndex:0];
SKPayment *payment = [SKPayment paymentWithProduct:selectedProduct];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Not Available"
message:@"No products to purchase"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp show];
[tmp release];
}
}
//the last step this is what is being called more and more times every time I do it (the uiView also pops up more times)
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// show wait view here
statusLabel.text = @"Verwerken...";
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock feature 2
statusLabel.text = @"Klaar!";
if (Temp == 2){
//ADD COINS WHEN PURCHASES IS COMPLETE
int coinsToAdd = 100;
int currentCoins = [[NSUserDefaults standardUserDefaults] integerForKey:@"savedstring"];
int savestring =
currentCoins + coinsToAdd;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setInteger:savestring forKey:@"savedstring"]; [defaults synchronize];
[label111 setText:[NSString stringWithFormat:@"Munten: %i",[self getCoins]]];
//ALERTVIEW TO SHOW YOU PURCHASED COINS
UIAlertView *tmp2 = [[UIAlertView alloc]
initWithTitle:@"Voltooid"
message:@"Je hebt 100 Munten Gekocht"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[tmp2 show];
[tmp2 release];
Thanks for reading hope someone can help me out 🙂
Once you made a transaction you should set transaction.transactionState to SKPaymentTransactionStatePurchased, otherwise you will repeat the transaction the next time.