Ser Pounce
Ser Pounce

Reputation: 14553

Possible to be notified when in app purchase completes?

Am wondering, is there a way to register for a notification for when an in app purchase completes? IE either when a purchase or restore fails or succeeds? I'd like to handle these four cases so I can display an alert to the user in the view controller where the in app purchase takes place. Does anyone know how to do this?

Upvotes: 0

Views: 83

Answers (1)

Jared Messenger
Jared Messenger

Reputation: 1236

(void)paymentQueue:updatedTransactions: is called when the transaction status updates. You could show the alert windows when finishing the transactions.

- (void) purchaseItem:(SKProduct *) item
{
    NSLog(@"Purchasing item %@", item.localizedTitle);
    SKPayment *payment = [SKPayment paymentWithProduct:item];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                break;
            default:
                break;
        }
    }
}

- (void) completeTransaction:(SKPaymentTransaction *) transaction
{
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Transaction"
                                                message:@"Your purchase is complete"
                                                 delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];
    [message show];

    // or 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishTransactionSuccessfully:) name:PURCHASE_NOTE object:nil];
}

Upvotes: 1

Related Questions