Ser Pounce
Ser Pounce

Reputation: 14553

displaying dialogue after in-app purchase complete

I have implemented the SKPaymentTransactionObserver in my apps AppDelegate the way Apple recommended:

- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:transaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:transaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // Optionally, display an error here.
    }

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    self.products = response.products;
}

- (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];
            default:
                break;
        }
    }
}

I'd like for my app to send a dialogue message to the user when the following happens: purchase successful, purchase failed, restore successful, restore failed. I'm having a little trouble wrapping my head around how I can do this with my design set up. I have a couple of questions:

1) The alert needs to be posted in the view controller where the transaction is initialized. How can I make the AppDelegate communicate with this view controller to let it know when an event has happened? Do I set up a delegate for the AppDelegate? This seems kind of funny to me...is there a better way?

2) Where do I send the message? Should it be in finishTransaction (do I need to override?) or somewhere else?

Upvotes: 0

Views: 155

Answers (1)

Apurv
Apurv

Reputation: 17186

Passing a notification will be the best way for it. The view controller which invokes the payment procedure should register for notification.

On the completion of a transaction, app delegate will post the notification which controller will receive and it will show the appropriate message.

Upvotes: 1

Related Questions