iPhone - in-App purchase consumable correct approach

Duck picture Duck · Jun 23, 2011 · Viewed 15.1k times · Source

I have this new app I am creating that will use consumable in-app purchases.

My question is this: how does that work? I mean, imagine the user buys a consumable stuff. So, after finalizing the sell I set a flag on the app's database authorizing the use of that object. I read somewhere that I have to provide the user with a button to restore old transactions in case of the user for some reason loses his device and has to restore everything.

Imagine the user has already used that purchase and after that he restores the old in-app purchases. What happens then? Will the user have the same resources again, so he can use a second time without paying? How it works and how should I approach that?

thanks

Answer

user1139733 picture user1139733 · Feb 1, 2012

I wanted to share a somewhat unorthodox solution I found to this problem that has the HUGE advantage of not requiring a server. This method allows users to restore their consumable items if the app is deleted and reinstalled, but does not allow them to move the items to a new device (unless all their app data is copied over).

Data stored in the keychain persists when an app is deleted and reinstalled. The keychain is intended for storing usernames and passwords, but you can also store information about consumable purchases in there. I used the KeychainItemWrapper class, available here: https://developer.apple.com/library/content/samplecode/GenericKeychain/Introduction/Intro.html

Here is some sample code where I store and retrieve the number of paid hints that a user has remaining:

//Storing the consumable hint item count
int hintsLeft = 100;
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Hints" accessGroup:nil];
NSString *hintsString = [NSString stringWithFormat:@"%i",hintsLeft];
[wrapper setObject:hintsString forKey:(id)kSecValueData];
[wrapper release];

//Retrieving it
KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Hints" accessGroup:nil];
NSString *numHints = [wrapper objectForKey:(id)kSecValueData];
[wrapper release];
int retrievedHints = [numHints intValue];

Notes:

  • the key (id)kSecValueData can't be an arbitrary string, there is a set list of constants that you can use as the key.

  • You will need to add the security framework