RyanM
RyanM

Reputation: 5700

iOS -- How to save in-app purchase data?

I'm planning on implementing some in-app purchase items and I want to save purchase information/data such that iTunes will backup said information when my customers sync. The Apple provided documentation states that this is possible, but doesn't really tell you how.

Where/how do I save purchase history (let's say, a NSString for each item) such that iTunes will back it up on the next sync?

Lastly, how would you suggest that I test this before making all of this go live?

Thanks.

P.S. Right now I'm using NSUserDefaults to store this info.

Upvotes: 6

Views: 5299

Answers (4)

While NSUserDefaults can be used to store purchase history, it's possible for a hacker to modify that and get access to paid features for free.

Keychain is a little better in that it's more persistent (will still be there after an app is removed and re-installed) but it's still possible for a really dedicated hack to add data to the keychain that makes your application think a purchase has been made.

You could also write out purchase info into some encrypted file you store within the app, that would be backed up with the app as well and might be more flexible.

The best approach is to store purchase data wherever is most convenient for your app, but then also check the receipt of the application that is stored in Bundle.main.appStoreReceiptURL to make sure what you have stored, Apple also considers to have been purchased.

That receipt is supposed to be sent through your own server to Apple, which returns receipt JOSN from the data stored at that appStoreReceiptURL.

For testing, although you can also use a sandbox iTunes account for testing on device, a newer method introduced with Xcode 12 is to use a StoreKit configuration where you can define all of your products without having to enter them in appStoreConnect first. When you run it will process purchase transactions locally, including within the simulator (not possible with the iTunes sandbox).

For more information on testing using the Xcode App Store Config files read this article:

https://www.namiml.com/blog/inapp-purchases-ios14-simulator

Upvotes: 0

ramzesenok
ramzesenok

Reputation: 6911

As already mentioned you can use UserDefaults for this purpose, but as Apple states it can also be achieved using iCloud and NSUbiquitousKeyValueStore

https://developer.apple.com/documentation/storekit/in-app_purchase/persisting_a_purchase

Testing can be easily done nowadays right within Xcode using .storekit configuration file.

https://developer.apple.com/documentation/storekit/in-app_purchase/testing_in-app_purchases_in_xcode

Upvotes: 0

sunkehappy
sunkehappy

Reputation: 9091

Actually NSUserDefaults is under

<Application_Home>/Library/Preferences

And the Library directory will be backed up by the iTunes.

Another way to save your in app purchase data (because it's very little) is to save it to keychain. Because keychain is safe and will be reserved when the app is deleted. So when user install the app again user can get their purchase instantly. Check this link:Lockbox: Easily Secure Your App’s Sensitive Data

Upvotes: 5

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

NSUserDefaults is fine for storing that kind of thing, but you should also use StoreKit's “check for purchased items” capability—a lot of apps that use in-app purchases have a button somewhere that checks the “purchased” state of the available products and re-enables them in the app, in case a user uninstalls and then reinstalls the app or installs it on a different device.

Testing in-app purchases can be done using the sandbox environment—iTunes Connect will let you set up a test iTunes Store account which has free access to all of your app's in-app purchases.

Upvotes: 11

Related Questions