简体   繁体   English

为什么从不调用 updateTransactions?

[英]Why is updatedTransactions never called?

After the user purchase the item, by entering email and password and paying, the method updatedTransactions is never called.用户购买商品后,通过输入电子邮件和密码并付款,从不调用方法 updatedTransactions。 The item is regularly bought but, without calling updatedTransactions methor I can't execute the code to add the item to the app.该项目是定期购买的,但是,如果不调用 updatedTransactions 方法,我将无法执行将项目添加到应用程序的代码。 I noticed that the item is bought because if I run the code to restore an item bought, everything goes fine and the product gets installed into the app.我注意到该物品已被购买,因为如果我运行代码来恢复购买的物品,一切正常,并且该产品会安装到应用程序中。

Here is the code I copied to buy the product:这是我复制的购买产品的代码:

-(IBAction)buyProduct:(id)sender {

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

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

}

Follows the code to restore the product:按照代码恢复产品:

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

I noticed that in this last piece of code I have the following line:我注意到在最后一段代码中,我有以下行:

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

Should I add this line in the buy part too?我也应该在购买部分添加这条线吗?

I solved this issue by adding the following code in the buyProduct method:我通过在 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];

}

Now the method updatedTransactions is called before making the payment and after the payment.现在,在付款之前和付款之后调用方法 updatedTransactions。

With the following code you can check if everything went good or bad:使用以下代码,您可以检查一切是否顺利:

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM