简体   繁体   中英

Why is updatedTransactions never called?

After the user purchase the item, by entering email and password and paying, the method updatedTransactions is never called. The item is regularly bought but, without calling updatedTransactions methor I can't execute the code to add the item to the app. 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:

-(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.

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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