I’ve implemented the In App purchase and restore purchases methods in my app.
I simply want to display an alert message if there are no previously purchased In App products available.
In the current example below, the payment method initializes soon after the restore request.
// Buy Button that initiates In App purchase request
-(IBAction)buyButtonPressed:(id)sender {
if (self.products != nil)
{
SKPayment *payment = [SKPayment paymentWithProduct:self.products];
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
[[SKPaymentQueue defaultQueue]addPayment:payment];
[self.activityIndicator startAnimating];
}
}
//Restore Button initiates In App Restore
- (IBAction)restoreButtonPressed:(id)sender {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
//Currently I’m displaying an alert here if there are no previous purchased products available.
if(queue.transactions.count == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"There is no restore available for your account" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[self.activityIndicator stopAnimating];
return;
}
NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
if ([productID isEqualToString:@"com.mydomain.app.product”])
{
//Unlocks the App
[self unlockApp];
[self.activityIndicator stopAnimating];
}
}
purchasedItemIDs = nil;
}
//Payment QUEUE
-(void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
{
[self.activityIndicator startAnimating];
break;
}
case SKPaymentTransactionStatePurchased:
{
[self unlockApp];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self.activityIndicator stopAnimating];
break;
}
case SKPaymentTransactionStateRestored:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self unlockApp];
[self showAlert:@"Application successfully restored"];
[self.activityIndicator stopAnimating];
break;
}
case SKPaymentTransactionStateFailed:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self.activityIndicator stopAnimating];
break;
}
case SKPaymentTransactionStateDeferred:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self.activityIndicator stopAnimating];
break;
}
default:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self.activityIndicator stopAnimating];
break;
}
}
}
In paymentQueueRestoreCompletedTransactionsFinished
check to see if queue.transactions.count == 0
. If it is, then there are no purchases to restore. For example:
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
// Check to see if there are purchases to restore
if (queue.transactions.count == 0) {
// No purchases to restore
// Show alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Purchases"
message:@"There are no purchases to restore."
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:nil];
[alert show];
}
else {
// There are purchases to restore
// Do something else
}
}