应用程序内购买:收听“取消”按钮?

时间:2012-02-08 22:56:39

标签: iphone ios xcode ipad in-app-purchase

我正在试图弄清楚当用户试图购买某些内容时,我会如何收听显示在“确认”警告中的“取消”按钮。你知道,苹果官方做的官方看起来像是:“确认你的应用内购买。你想以价格购买一个$产品吗?[取消] [购买]”

如果我正确理解了我的代码,则通过以下内容发起警报:

SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];

所以基本上我想做点什么,如果他们点击取消。感谢

2 个答案:

答案 0 :(得分:9)

实施SKPaymentTransactionObserver Protocol中的paymentQueue:updatedTransactions:方法。在那里,您可以查看每个transactionState对象的errortransaction

我使用了类似的东西:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                if (transaction.error.code == SKErrorPaymentCancelled) {
                    /// user has cancelled
                    [self finishTransaction:transaction wasSuccessful:NO];
                }
                else if (transaction.error.code == SKErrorPaymentNotAllowed) {
                    // payment not allowed
                    [self finishTransaction:transaction wasSuccessful:NO];
                }
                else {
                    // real error
                    [self finishTransaction:transaction wasSuccessful:NO];
                    // show error
                }
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                break;
            default:
                break;
        }
    }
}

答案 1 :(得分:-1)

使用类似的东西:

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                if (transaction.error.code == SKErrorPaymentCancelled) {
                    /// user has cancelled
                    [self finishTransaction:transaction wasSuccessful:NO];
                }
                else if (transaction.error.code == SKErrorPaymentNotAllowed) {
                    // payment not allowed
                    [self finishTransaction:transaction wasSuccessful:NO];
                }
                else {
                    // real error
                    [self finishTransaction:transaction wasSuccessful:NO];
                    // show error
                }
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
                break;
            default:
                break;
        }
    }
}