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?

Answers


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

    }

Need Your Help

How to debug memory leaks when Leaks instrument does not show them?

ios swift memory-management memory-leaks automatic-ref-counting

I have an iOS app written in Swift that is leaking memory - in certain situation some objects should be released but they are not. I have learnt about the issue by simply adding deinit debug messages

Appending to an array vs writing to file

java arrays file memory-management

I'm writing a program which takes in a byte array of potentially millions of bytes, reads each one from a ByteArrayInputStream, and if the byte is not "printable" (ascii 32-126), that byte is encod...