Reputation: 3914
I am having an app in which I have 5 Non Consumable In App Purchases.
I am using the following code for Restore In App Purchase.
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
//NSLog(@"into method1");
purchasedItemIDs = [[NSMutableArray alloc] init];
NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
}
// [customSpinner hide:YES];
if (queue.transactions.count==0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"FameFace" message:@"It seems you have not buy this Package" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
else
{
NSLog(@"Data available");
NSString *isInApp = [[NSUserDefaults standardUserDefaults]valueForKey:@"Package"];
if ([isInApp isEqualToString:@"Pack1"])
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
NSLog(@"Launching first time");
requestText= @"Animal";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([isInApp isEqualToString:@"Pack2"])
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
NSLog(@"Launching first time");
requestText= @"Celebrity";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([isInApp isEqualToString:@"Pack3"])
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
NSLog(@"Launching first time");
requestText= @"Nature";
[self MakeQueryForUpdate];
[self getdata];
}
}
}
Below is the code when there is successful transaction.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
//[self downloadFromURL:[NSURL URLWithString:@"http://www.iphonedevnation.com/tutorials/ForestGreen.mp3"]];
// flgCheck = 1;
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self completeTransaction:transaction];
NSString *isInApp = [[NSUserDefaults standardUserDefaults]valueForKey:@"Package"];
if ([isInApp isEqualToString:@"Pack1"])
{
//by manthan...7th may
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
NSLog(@"Launching first time");
requestText= @"Animal";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([isInApp isEqualToString:@"Pack2"])
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
NSLog(@"Launching first time");
requestText= @"Celebrity";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([isInApp isEqualToString:@"Pack3"])
{
//by manthan... 7th may
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
NSLog(@"Launching first time");
requestText= @"Nature";
[self MakeQueryForUpdate];
[self getdata];
}
}
This is working fine.
What I am doing is that I am inserting some images in my DB when user Purchases a package from In App Purchase.
But as the Product is Non Consumable, If the user deletes the app and trying to Restore Images, My DB doesn't get updated.
Any help will be appreciated.
Thanks...
Edited
I put my code in the method below when user try to restore purchase.
As I have a single restore button and on pressing that button, user will have previously purchased packages that may be 2,3 or 4(any of the 5 packages that user has purchased once as these are non consumable products).
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSString *pk = [[NSUserDefaults standardUserDefaults]valueForKey:@"Package"];
if ([pk isEqualToString:@"Pack1"])
{
//by manthan...7th may
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
NSLog(@"Launching first time");
requestText= @"Animal";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([pk isEqualToString:@"Pack2"])
{
//by manthan... 7th may
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
NSLog(@"Launching first time");
requestText= @"Celebrity";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([pk isEqualToString:@"Pack3"])
{
//by manthan... 7th may
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
NSLog(@"Launching first time");
requestText= @"Nature";
[self MakeQueryForUpdate];
[self getdata];
}
}
when there is only one single purchase I can simply update my DB in SKPaymentTransactionStateRestored.
But when i have multiple non consumable products what should i do?
edited
if ([transaction.payment.productIdentifier isEqualToString:@"Pack1"])
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp1Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp1Once"];
NSLog(@"Launching first time");
requestText= @"Animal";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([transaction.payment.productIdentifier isEqualToString:@"Pack2"])
{
//by manthan... 7th may
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp2Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp2Once"];
NSLog(@"Launching first time");
requestText= @"Celebrity";
[self MakeQueryForUpdate];
[self getdata];
}
}
else if ([transaction.payment.productIdentifier isEqualToString:@"Pack3"])
{
//by manthan... 7th may
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedp3Once"])
{
// app already launched
NSLog(@"Launching secondtime");
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedp3Once"];
NSLog(@"Launching first time");
requestText= @"Nature";
[self MakeQueryForUpdate];
[self getdata];
}
}
Upvotes: 0
Views: 1556
Reputation: 107131
You need to use the case of SKPaymentTransactionStateRestored
in the updatedTransactions
method for doing this.
When restoring this case will be executed.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction * transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
//do your stuff
break;
case SKPaymentTransactionStateFailed:
//do your stuff
break;
case SKPaymentTransactionStateRestored:
//do your database stuff
default:
break;
}
};
}
Payment Transaction States
The state of a transaction.
enum { SKPaymentTransactionStatePurchasing,
SKPaymentTransactionStatePurchased, SKPaymentTransactionStateFailed,
SKPaymentTransactionStateRestored };
typedef NSInteger SKPaymentTransactionState;
Constants
SKPaymentTransactionStatePurchasing
The transaction is being processed by the App Store.
SKPaymentTransactionStatePurchased
The App Store successfully processed payment. Your application should provide the content the user purchased.
SKPaymentTransactionStateFailed
The transaction failed. Check the error property to determine what happened.
SKPaymentTransactionStateRestored
This transaction restores content previously purchased by the user. Read the originalTransaction property to obtain information about the original purchase. Available in iOS 3.0 and later. Declared in SKPaymentTransaction.h.
Check SKPaymentTransaction for more
Upvotes: 2