iOS——如何保存应用内购买数据?
我计划实现一些应用内购买项目,并且我想保存购买信息/数据,以便 iTunes 在我的客户同步时备份所述信息。苹果提供的文档指出这是可能的,但并没有真正告诉你如何做。
我在哪里/如何保存购买历史记录(比方说,每个项目都有一个 NSString),以便 iTunes 在下次同步时备份它?
最后,您建议我如何在上线之前对其进行测试?
谢谢。
PS 现在我正在使用 NSUserDefaults 来存储此信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSUserDefaults
适合存储此类内容,但您还应该使用 StoreKit 的“检查已购买项目”功能 - 许多使用应用内购买的应用程序在某处都有一个按钮,用于检查“已购买项目” ”可用产品的状态,并在应用程序中重新启用它们,以防用户卸载然后重新安装应用程序或将其安装在不同的设备上。可以使用沙盒环境来测试应用内购买 - iTunes Connect 将允许您设置一个测试 iTunes Store 帐户,该帐户可以免费访问您应用程序的所有应用内购买。
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.
其实
NSUserDefaults
在Library
目录下,会被iTunes备份。保存应用内购买数据的另一种方法(因为它很少)是将其保存到钥匙串。因为钥匙串是安全的,并且在删除应用程序时会被保留。因此,当用户再次安装该应用程序时,用户可以立即购买。检查此链接:密码箱:轻松保护您的应用程序敏感数据
Actually
NSUserDefaults
is underAnd 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
正如已经提到的,您可以使用 UserDefaults 来实现此目的,但正如 Apple 所说,也可以使用 iCloud 和 NSUbiquitousKeyValueStore 来实现
https://developer.apple.com/documentation/storekit/in-app_purchase/persisting_a_purchase
现在可以轻松完成测试,对吧在 Xcode 中使用
.storekit
配置文件。https://developer.apple.com/documentation/storekit/in- app_purchase/testing_in-app_purchases_in_xcode
As already mentioned you can use
UserDefaults
for this purpose, but as Apple states it can also be achieved using iCloud andNSUbiquitousKeyValueStore
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
虽然 NSUserDefaults 可用于存储购买历史记录,但黑客有可能对其进行修改并免费访问付费功能。
钥匙串要好一点,因为它更持久(删除并重新安装应用程序后仍然存在),但仍然有可能真正专门的黑客将数据添加到钥匙串,使您的应用程序认为已进行购买。
您还可以将购买信息写入存储在应用程序中的一些加密文件中,该文件也将与应用程序一起备份,并且可能会更灵活。
最好的方法是将购买数据存储在对您的应用程序来说最方便的地方,但同时还要检查存储在 Bundle.main.appStoreReceiptURL 中的应用程序收据,以确保您存储的内容,Apple 也认为已购买。
该收据应该通过您自己的服务器发送到 Apple,Apple 从存储在该 appStoreReceiptURL 的数据中返回收据 JOSN。
对于测试,虽然您也可以使用沙盒 iTunes 帐户在设备上进行测试,但 Xcode 12 引入的一种更新方法是使用 StoreKit 配置,您可以在其中定义所有产品,而无需先在 appStoreConnect 中输入它们。当您运行时,它将在本地处理购买交易,包括在模拟器中(iTunes 沙箱不可能)。
有关使用 Xcode App Store 配置文件进行测试的更多信息,请阅读本文:
https: //www.namiml.com/blog/inapp-purchases-ios14-simulator
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