在应用内购买完成后显示对话

时间:2013-07-06 12:14:52

标签: iphone ios cocoa-touch in-app-purchase

我已按照Apple推荐的方式在我的应用SKPaymentTransactionObserver中实施了AppDelegate

- (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;
        }
    }
}

我希望我的应用在发生以下情况时向用户发送对话消息:购买成功,购买失败,恢复成功,恢复失败。我的设计设置如何能够解决这个问题,我遇到了一些麻烦。我有几个问题:

1)警报需要在初始化事务的视图控制器中发布。如何使AppDelegate与此视图控制器通信,以便在事件发生时通知它?我是否为AppDelegate设置了代理?这对我来说似乎很有趣......有更好的方法吗?

2)我在哪里发送消息?它应该在finishTransaction(我需要覆盖?)还是在其他地方?

1 个答案:

答案 0 :(得分:1)

传递通知将是最佳方式。调用付款程序的视图控制器应该注册通知。

在交易完成后,app delegate将发布控制器将收到的通知,并显示相应的消息。