Reputation: 79
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.
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.
Upvotes: 0
Views: 481
Reputation: 18922
Your delegate should implement this method:
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
The error in this case will indicate a cancel action.
Upvotes: 1