为什么从不调用updatedTransactions?

时间:2015-11-24 14:09:20

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

用户购买商品后,通过输入电子邮件和密码并付款,从不调用updatedTransactions方法。该项目是定期购买的,但没有调用updatedTransactions方法我不能执行代码将项目添加到应用程序。我注意到该商品已被购买,因为如果我运行代码来恢复购买的商品,一切都会顺利进行,并且产品会安装到应用中。

以下是我为购买该产品而复制的代码:

-(IBAction)buyProduct:(id)sender {

    NSLog(@"Purchasing: %@", self.product.productIdentifier);

    SKPayment *payment = [SKPayment paymentWithProduct:self.product];
    [[SKPaymentQueue defaultQueue] addPayment:payment];

}

按照代码恢复产品:

-(IBAction)restoreProduct:(id)sender {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

我注意到在最后一段代码中我有以下几行:

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

我是否应该在购买部分添加此行?

1 个答案:

答案 0 :(得分:1)

我通过在buyProduct方法中添加以下代码解决了这个问题:

-(IBAction)buyProduct:(id)sender {

    NSLog(@"Purchasing: %@", self.product.productIdentifier);

    SKPayment *payment = [SKPayment paymentWithProduct:self.product];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; //added line
    [[SKPaymentQueue defaultQueue] addPayment:payment];

}

现在,在付款之前和付款之后调用updatedTransactions方法。

使用以下代码,您可以检查一切是好还是坏:

for(SKPaymentTransaction *transaction in transactions) {
        if (transaction.transactionState == SKPaymentTransactionStatePurchased) {
            [self unlockRemoveiAD];
            [OKalertView show];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        }

        if (transaction.transactionState == SKPaymentTransactionStateFailed) {
            [alertView show];
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
        }

    }