简体   繁体   English

在iOS中管理SKProductsRequest的最佳方法是什么?

[英]What's the best way to manage SKProductsRequests in iOS?

I'm working on an app that offers in-app purchases of non-consumable items. 我正在开发一个提供非消耗性物品的应用程序内购买的应用程序。

The non-consumables are built into the app, as opposed to being downloaded from a server, so actually we're just granting access to the content. 非耗材内置于应用程序中,而不是从服务器下载,因此实际上我们只是授予对内容的访问权限。

Our app: 我们的应用程序:

  • shows a Netflix style library of content - some is free and some is purchasable. 显示了Netflix样式的内容库-有些是免费的,有些是可购买的。
  • must work without an internet connection (although one is required for purchases, obvs.) 必须在没有互联网连接的情况下工作(购买时需要一个连接,观察)。
  • should show the purchasable content within the library, even when there is no Internet connection 即使没有Internet连接,也应在库中显示可购买的内容

Questions: 问题:

When is the best time to call start on the SKProductsRequest to fetch the product into etc. from iTunes Connect? 什么时候是在SKProductsRequest上调用start的最佳时间,以便从iTunes Connect将产品提取到其他内容中? In application:didFinishLaunchingWithOptions: ? application:didFinishLaunchingWithOptions: Or in my controller when it's loaded? 还是在我的控制器加载时?

  • We want to show the product price on screen, but also avoid a situation where the product prices have changed, and the User's app has not updated the price on screen. 我们希望在屏幕上显示产品价格,但也要避免产品价格发生变化且用户的应用程序尚未在屏幕上更新价格的情况。

How frequently should I update the product info? 我应该多久更新一次产品信息? Whenever the screen loads? 屏幕何时加载? Whenever it appears? 每当它出现? When the app is started? 应用何时启动?

A good solution would be to implement Push Notifications and whenever you change the prices, broadcast a push notification to the app, Which would fetch new prices! 一个好的解决方案是实施推送通知,并且每当您更改价格时,将推送通知广播到应用程序,这将获取新的价格!

Update: If internet is not available at that time, then you can invalidate current prices so that when app is launched again it updates the prices! 更新:如果当时无法使用互联网,则可以使当前价格无效,以便在再次启动应用程序时更新价格! You can store a BOOL in NSUserDefaults which tells if the prices are up to date or not. 您可以将BOOL存储在NSUserDefaults ,以告知价格是否最新。

I came up with my own solution to this problem using NSNotificationCenter. 我使用NSNotificationCenter提出了自己的解决方案。

Basically, my process is this: 基本上,我的过程是这样的:

  • When the app starts, tell my custom ProductList class to update the list of products. 应用启动时,告诉我自定义的ProductList类以更新产品列表。
  • When the app's internet connection state changes (to has connection), tell the ProductList class to update the list of products. 当应用程序的Internet连接状态更改(具有连接状态)时,告诉ProductList类更新产品列表。
  • The ProductList method update_all will only perform the iTunesConnect request if: 在以下情况下, ProductList方法update_all将仅执行iTunesConnect请求:
    • The array of SKProducts I'm storing is empty (we don't already have the product info) 我要存储的SKProducts数组为空(我们尚无产品信息)
    • OR The list of products hasn't been updated in a day or longer (the info may be stale) 或产品列表在一天或更长时间内未更新(信息可能已过时)

Since I wanted to handle a situation where a user started the device without internet connection, and loaded the purchasable content before connecting, I set up an observer. 由于我想处理用户在没有互联网连接的情况下启动设备并在连接之前加载可购买内容的情况,因此我设置了一个观察者。

Once the Products have been retrieved from iTunes connect, I send out an NSNotification to any views that may be observing, so they can update the prices etc. 从iTunes connect中检索到产品后,我将向所有可能观察到的视图发送NSNotification,以便它们可以更新价格等。

Hope that makes sense and helps somebody? 希望这有意义并能帮助到别人吗?

If you want to show prices for your In App Purchase dynamically then I think you should not call the methods in application didFinishLaunchingWithOptions. 如果您想动态显示应用程序内购买的价格,那么我认为您不应在应用程序didFinishLaunchingWithOptions中调用方法。

As it is going to get called every time only when your app launches. 因为只有在应用启动时才会被调用。 If your app is running and you change the price tier of the app at that time then the user will not be able to see that change at that time. 如果您的应用程序正在运行,并且您当时更改了该应用程序的价格层,则该用户当时将无法看到该更改。 User will have to launch the app again. 用户将不得不再次启动该应用程序。

So you should call it from your viewcontroller's ViewWillAppear method. 因此,您应该从viewcontroller的ViewWillAppear方法中调用它。 Thats what I did for one of my projects. 那就是我为我的一个项目所做的。

So if you change the price tier from itunes whenever, then even if your app is running then whenever user comes to that view, changes will be seen. 因此,如果您随时从iTunes更改价格层,那么即使您的应用正在运行,只要用户进入该视图,都将看到更改。

Hope it helps you. 希望对您有帮助。

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

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