简体   繁体   中英

How to Update UI when user cancelled IAP ios sdk

I have successfully implemented IAP in ios sdk.

But the problem is when user click on Restore purchase button I initiate the restore by this code:

 [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

And also I have written this code to handle and initiate the purchase :

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
    switch(transaction.transactionState){

        case SKPaymentTransactionStatePurchasing: NSLog(@"Transaction state -> Purchasing");
            //called when the user is in the process of purchasing, do not add any of your own code here.
            break;

        case SKPaymentTransactionStatePurchased:
            //this is called when the user has successfully purchased the package 
              [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
             [SVProgressHUD dismiss];
            [self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads


            break;
        case SKPaymentTransactionStateRestored:
             [SVProgressHUD dismiss];
            NSLog(@"Transaction state -> Restored");
            //add the same code as you did from SKPaymentTransactionStatePurchased here
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
        case SKPaymentTransactionStateFailed:
             [SVProgressHUD dismiss];

            //called when the transaction does not finish
            if(transaction.error.code == SKErrorPaymentCancelled){
                [SVProgressHUD dismiss];

                NSLog(@"Transaction state -> Cancelled");
                //the user cancelled the payment ;(
            }
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            break;
    }
}
 }

When I select Restore then A dialogs come which ask for password. When I cancel it The progress hud doesn't hides.

How can I do this. How to update UI when user cancel purchase in between the process.

Here is the image. 在此处输入图片说明

EDIT now when i use the delegate method

- (void)paymentQueue:(SKPaymentQueue *)queue   restoreCompletedTransactionsFailedWithError:(NSError *)error
{
 [SVProgressHUD dismiss];

}

When It asks for password the SVProgressHUD hides. No matter I press cancel or OK. How to handle this.

And How to update the UI and continue Purchase when user enters correct password.

Your delegate should implement this method:

- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error

The error in this case will indicate a cancel action.

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