问题描述
我正在为我的应用程序开发一个很棒的功能,它需要在后台定期发送HTTP请求。
I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.
我已经阅读了很多文章和讨论,但是我没有提出解决方案,因为我在上遇到的所有建议都只能部分解决问题。
I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.
让我详细解释一下我希望我的应用程序做什么。
Let me explain in details what do I want my application to do.
考虑有一个带有API的网站,我想发送请求(当应用程序处于后台模式)定期检查网站上的数据状态。如果数据状态是可接受的,我会向用户发送推送通知,以通知他此时数据是正确的。
Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.
有人可以提出解决方案如何实现这一点在iOS应用程序中的想法?
Could someone propose a solution how to implement this idea in iOS app?
推荐答案
在iOS上你不能这样做,正如你所描述的那样。当您的应用处于后台或未运行时,您无法定期安排任务。您也无法控制iOS是否在后台运行时决定终止您的应用程序,因此不运行是您需要处理的情况(您不能只在后台继续运行如你所愿)。
On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).
我不确定我是否了解服务器端的情况。如果您的服务器正在操作数据直到它可以接受,并且它可以发送推送通知,为什么它需要等待来自手机的传入请求?为什么不在数据就绪时发送推送?如果应用程序决定什么是可接受的,可能让应用程序告诉服务器它想要什么,以便服务器知道何时发送推送。
I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.
有几个选项将接近您描述的内容。如果您在应用中实施后台获取功能,iOS将在未运行时启动该应用,并让它在后台进行网络呼叫。但是,不能保证这种情况发生的频率。这在
There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs
另一个选项是静音推送通知。如果您的服务器发送其中一个,iOS可以在后台启动应用程序来处理通知。如有必要,该应用可以进行网络通话。您可以随时发送这些内容,但Apple警告不要过度:
The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:
无声推送在中有所描述。
Silent pushes are described in Apple's push notification docs.
这篇关于定期在后台模式(iOS)中发送HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!