简体   繁体   English

iOS — 如何保存应用内购买数据?

[英]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.我计划实施一些应用内购买项目,我想保存购买信息/数据,以便 iTunes 在我的客户同步时备份所述信息。 The Apple provided documentation states that this is possible, but doesn't really tell you how. Apple 提供的文档指出这是可能的,但并没有真正告诉你如何。

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?我在哪里/如何保存购买历史记录(比方说,每个项目的 NSString),以便 iTunes 在下一次同步时备份它?

Lastly, how would you suggest that I test this before making all of this go live?最后,你会如何建议我在让所有这些上线之前测试这个?

Thanks.谢谢。

PS Right now I'm using NSUserDefaults to store this info. PS 现在我使用 NSUserDefaults 来存储这个信息。

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. NSUserDefaults可以很好地存储这种东西,但你也应该使用 StoreKit 的“检查购买的物品”功能——许多使用应用内购买的应用程序在某处都有一个按钮,用于检查可用产品的“已购买”状态和在应用程序中重新启用它们,以防用户卸载然后重新安装应用程序或将其安装在不同的设备上。

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.可以使用沙盒环境测试应用内购买——iTunes Connect 将允许您设置一个测试 iTunes Store 帐户,该帐户可以免费访问您应用的所有应用内购买。

Actually NSUserDefaults is under实际上NSUserDefaults

<Application_Home>/Library/Preferences

And the Library directory will be backed up by the iTunes.并且Library目录将由 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检查此链接: 密码箱:轻松保护应用程序的敏感数据

As already mentioned you can use UserDefaults for this purpose, but as Apple states it can also be achieved using iCloud and NSUbiquitousKeyValueStore如前所述,您可以为此使用UserDefaults ,但正如 Apple 所说,它也可以使用 iCloud 和NSUbiquitousKeyValueStore来实现

https://developer.apple.com/documentation/storekit/in-app_purchase/persisting_a_purchase 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.现在可以使用.storekit配置文件在 Xcode 中轻松完成测试。

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

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.尽管 NSUserDefaults 可用于存储购买历史记录,但黑客可以对其进行修改并免费访问付费功能。

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.最好的方法是将购买数据存储在对您的应用最方便的地方,然后还要检查存储在 Bundle.main.appStoreReceiptURL 中的应用的收据,以确保您存储的内容,Apple 也认为已购买。

That receipt is supposed to be sent through your own server to Apple, which returns receipt JOSN from the data stored at that appStoreReceiptURL.该收据应该通过您自己的服务器发送到 Apple,Apple 从存储在该 appStoreReceiptURL 的数据中返回收据 JOSN。

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.对于测试,虽然您也可以使用沙盒 iTunes 帐户在设备上进行测试,但 Xcode 12 引入的一种较新方法是使用 StoreKit 配置,您可以在其中定义所有产品,而无需先在 appStoreConnect 中输入它们。 When you run it will process purchase transactions locally, including within the simulator (not possible with the iTunes sandbox).当您运行时,它将在本地处理购买交易,包括在模拟器内(iTunes 沙盒无法实现)。

For more information on testing using the Xcode App Store Config files read this article:有关使用 Xcode App Store 配置文件进行测试的更多信息,请阅读本文:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM