iphone - 在后台连接到服务器
我正在创建一个连接到服务器并发送一些文本的应用程序。 如果有网络(wifi 或 3g),它会立即将文本发送到服务器。 但如果没有网络,它会每 5 分钟持续轮询一次服务器连接。 所有这部分都工作正常。
但是当使用 iPhone 4 设备时,我希望应用程序检查服务器连接,即使应用程序进入后台也是如此。因此,当应用程序进入后台并且网络恢复时,它必须能够将文本发送到服务器。
我怎样才能实现它?我见过一些应用程序,他们说该应用程序即使在后台也会将照片上传到服务器。他们将如何做?
I'm creating an app which connects to server and sends some text.
If network (both wifi or 3g) is there, it will immediately send the text to server.
But if there is no network, it keeps on polling for server connection every 5 minutes.
All this part is working fine.
But when using iPhone 4 device, i want the app to check for server connection even when app goes into background. So, when app goes to background and when network comes back, it must be able to send the text to server.
How can I achieve it? I've seen some apps where they say that the app will upload photos to server even in background. How will they do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议您仔细阅读 Apple 的这篇文章,尤其是在后台完成有限长度任务部分。
http://developer.apple.com/ library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
需要澄清的事情:
希望有帮助。
I suggest you read this article from Apple carefully, especially the Completing a Finite Length Task in the Background section.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
Something to clarify:
Hope it helps.
我认为 flayvr 正在使用一个技巧。
如果您下载并使用该应用程序,您会看到他们要求您启用您的位置。
这是为什么呢?
因为他们希望像您一样在后台执行某些操作,即使应用程序终止(他们用您新拍摄的照片创建相册),他们是如何做到的?
他们使用显着位置变化,当有人旅行一段显着距离(例如 500m)时,注册显着位置变化的每个应用程序将在有限的时间内被唤醒以执行一些快速任务,并将在几秒钟内终止。
因此,您的应用程序也可以注册该事件,并且当触发重大位置更改事件时,您将能够(快速)将文本发送到服务器。
希望有帮助。
There is a trick that I think flayvr is using.
If you download and use the app, you will see that they require you to enable your location.
And why is that?
because they want like you to do something in the background even when the app is terminated (they creating an album out of your newly captured photos), and how do they do that?
They use the significant location change, where when someone is traveling some significant distance (something like 500m) each app that registered for significant location change will get awaken for a limited amount of time to perform some quick task and will be terminated in a few seconds.
So your app can register to that event also and when the event of significant location change fired you will be able to send the text to server (quickly).
Hope that helps.
到目前为止,您可以在 iOS7 上使用后台获取来实现这一点。
看看这篇文章。
但是您只有最多 30 秒的时间来完成任务。
根据上面的文章,还有另一种解决方案,称为后台传输服务。
Until now you can do that on iOS7 with Background Fetch.
Take a look at this article.
However you only have up to 30s to get the task done.
According to the article above, there's also another solution called Background transfer service.
在 Xcode 中创建一个新项目,您将看到应用程序委托文件中自动生成了一堆新方法。如 applicationDidEnterBackground、applicationWillEnterForeground 等。
请阅读您必须调用线程在服务器上上传数据的描述。
Create a new project in Xcode and you will see there are bunch of new methods auto generated in app delegate file. like applicationDidEnterBackground, applicationWillEnterForeground etc.
read the description you have to call your thread to upload data on server here.