Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining records of iOS in-app-purchases

I've implemented IAPs where there's a one time purchase that unlocks some feature in the app. Since the purchase is made only once by each user I can just ask Apple's IAP service if this user has the made the purchase. If so then unlock the feature. If not then display the IAP prompt. I probably also persisted some "userDidMakePurchase" flag in user defaults as an imperfect solution to using the app w/o a network connection.

Now I'd like to implement an IAP where the user can make it over and over. Each time the user is given ten tokens to use an in-app currency. How can I determine the number of tokens a particular user has when they start the app?

So far I've considered persisting a token count in user defaults. This has some major downsides like deleting and reinstalling the app wipes out the user's token balance. Also tokens won't carry over to other devices.

Of course I could persist the token counts on my own server or use AWS. However I'd like to avoid this if possible. I'm only familiar with IAP basics so perhaps Apple keeps track of all purchases and I can just figure it out from there.

like image 856
SundayMonday Avatar asked Oct 29 '12 20:10

SundayMonday


1 Answers

Switching from "buy once, use forever" to "buy many times, use while it lasts" makes your purchase a consumable in Apple's terminology. Since Apple does not store the state of consumable purchases for you, the task of keeping track of consuming the resource is entirely up to your application.

In general, there are four places that you could potentially use to store the remaining count:

------------------------------------------------------------------
|      Storage     | Offline? | Reinstall? | Encrypted? | Large? |
------------------------------------------------------------------
| Documents Folder |    YES   |     NO     |     NO     |   YES  |
| iCloud           |     NO   |     YES    |     NO     |   YES  |
| User Defaults    |    YES   |     NO     |     NO     |   NO   |
| Keychain         |    YES   |     YES    |     YES    |   NO   |
------------------------------------------------------------------

Since you need to store a tiny amount of data, your ideal candidate is Keychain: it works offline, survives reinstalls, and as an added bonus, it is also encrypted.

Although there is no built-in NS wrapper around the keychain CF APIs, Apple has kindly published a nice wrapper for it (scroll to the bottom of the page to see the code). Add security framework to your solution, copy-paste the wrapper code into your project (it's only two files). Now you can use keychain as if it were an NSDictionary.

like image 60
Sergey Kalinichenko Avatar answered Oct 28 '22 22:10

Sergey Kalinichenko