iPhone - in-app purchase : recording the purchase
I've read here and elsewhere the following thing :
Finally, after providing the feature, you should “remember” that the user has purchased the app. App开发者_如何学Gole’s recommended way is to use NSUserDefaults, the same way you store your settings.
But... If I'm not wrong, when you delete an app from your phone (but not from iTunes), and if you restore it later from iTunes, the prefs are deleted and restored to their default values. Is that right ? So am I right saying that doing this way, if the user deletes the app from its phone and some weeks later reinstall it from its iTunes library, its in-app purchases will be lost ?
Yes, you are correct. When the user deletes the app the NSUserDefaults is erased too. However, the app store has a handy restore purchases feature, where you can request to be told the purchases that apple has recorded for the current user.
There doesn't seem to be a good method of detecting that purchases need to be restored (at least from what I've seen on here), most developers seem to simply offer a button that starts the restore process manually.
There is an alternative, which is to store the user purchases in the Keychain, which is (at least right now) not deleted when the app is uninstalled. I've confirmed this is the case on iOS 4.3 - though it may not stay that way forever.
Here's some sample code that demonstrates both restore purchases, and using the keychain to save purchases
精彩评论