为什么从不调用 updatedTransactions?

Why is updatedTransactions never called?

用户购买商品后,通过输入电子邮件和密码并付款,永远不会调用 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];

我是否也应该在购买部分添加这一行?

我通过在 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];
        }

    }