IPhone : Restore In App Purchases on New Devices / Deleted Apps on the same device using restoreCompletedTransactions

on Monday, March 30, 2015

I have an app in the App Store and I'm bringing IAPs to unlock features in the new version. I have implemented the IAP mechanism and all purchases are working well.


When the user purchases a product, I set a NSUserDefault flag to then use that to unlock features elsewhere in the app.


That works well for me.


My question though is around restoring.


I have seen lots of questions on this but it's not quite clear to me what the solution is.


If the user purchases the IAP, it stores the NSUserDefault flag. However, if the user uses a different device with the same Apple ID, or deletes and reinstalls the app, the restore purchase button doesn't detect the previously saved NSUserDefault (understandably).



- (void)restoreThePurchase
{
// Similar to the purchase
// Ask the App Store to restore rather than create a payment. We call it via the SKPaymentQueue.
// When we've sent that ot the App Store, the App Store will get back to the App Delegate and get back to the paymentQueue updated:Transactions to the restore.
[[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
}


I have three classes:



  • IAPViewController which contains the purchase and restore UIButton

  • e100EntriesPurchase which contains the IAP methods like make a purchase and restore.

  • eUnlimitedEntriesPurchase which contains the IAP methods for the unlimited purchase along with the make purchase and restore.


When the restore button is pressed, I check which IAP the device had and then call the appropriate restore method.



- (IBAction)iapRestorePurchaseButton:(id)sender
{
// When the restorePurchase button is tapped, we will run a few tests.

if (([[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersion100Entries"]) && (![[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersionUnlimitedEntries"]))

{
[self.e100EntriesPurchase restoreThePurchase];
NSLog(@"100 yes but unlmited no restore!!!!!!!!!!!!!!!!!!!!!");
}
else if ((![[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersion100Entries"]) && ([[NSUserDefaults standardUserDefaults] boolForKey:@"FullVersionUnlimitedEntries"]))
{
[self.eUnlimitedEntriesPurchase restoreThePurchase];


}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Restore Unsuccessful" message:@"Your Apple ID doesn't have an active purchase for upgrading to Pro. " delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil, nil];
[alertView show];
}

}


So I'm facing the UIAlertView when I delete and reinstall the App because the NSUserDefaults will not be set to anything.


I'm not sure how exactly to do this, but I suspect when I make the purchase, I'll have to make more than just a NSUserDefault and need to save some sort of receipt?


Any guidance on this issue would really be helpful.


0 comments:

Post a Comment