5

I am writing code for in-app purchases and using a "Processing..." view with an activity indicator to block the "Buy Now" button once a purchase is initiated. However, how can you tell when the user hits a "Cancel" button since those alert views are coming from the AppStore.app?

Is there a delegate method that gets called when those cancel buttons are pressed? Or is it a matter of your view becoming firstResponder again? What am I missing here?

If you don't think this is possible, have a look at the "I Am T-Pain" app... they do something very similar and dismiss their view immediately after the cancel button is pressed.

alt text

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195

1 Answers1

9

Assuming everything is setup correctly you should have an object implementing SKPaymentTransactionObserver which will receive callbacks for transaction success/failure/cancel.

In my example it's the purchaseManager object mentioned in this call

  [[SKPaymentQueue defaultQueue] addTransactionObserver:purchaseManager];

When the user cancels a payment you should receive a callback with a transaction state of cancelled:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;

            case SKPaymentTransactionStateFailed:
                // THIS IS THE STATE YOU SHOULD SEE
                [self failedTransaction:transaction];
                break;

                           ...
}

You can use this callback to dismiss your view etc...

gdawg
  • 721
  • 6
  • 5
  • I honestly don't know how I missed that.... I was checking for cancel within a `failedTransaction:` if statement and never realized that it never got into that if statement. I guess all it takes is another pair of eyes on the situation. Thanks man! I really appreciate that – iwasrobbed Aug 13 '10 at 12:01
  • How does this solve the problem? There is still no distinct "Cancelled" state here so how do we know if the transaction failed or was cancelled before being issued? – Stavash Feb 12 '13 at 15:02
  • 4
    Once you have a failed transaction check if (transaction.error.code == SKErrorPaymentCancelled) . . . – codeghost Jul 04 '13 at 16:49
  • Unfortunately, when I cancel the error I see in the debugger is error code 2: "Cannot connect to iTunes store". – farhadf Dec 02 '15 at 00:21