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.
我正在创建一个连接到服务器并发送一些文本的应用程序。如果有网络(无论是wifi还是3g),它会立即将文本发送到服务器。但是如果没有网络,它会每隔5分钟继续轮询服务器连接。这一切都很好。
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.
但是当使用iPhone 4设备时,即使应用程序进入后台,我也希望应用程序检查服务器连接。因此,当应用程序进入后台并且当网络返回时,它必须能够将文本发送到服务器。
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?
我怎样才能实现它?我见过一些应用程序,他们说该应用程序即使在后台也会将照片上传到服务器。他们将如何做到这一点?
4 个解决方案
#1
13
I suggest you read this article from Apple carefully, especially the Completing a Finite Length Task in the Background section.
我建议你仔细阅读Apple的这篇文章,特别是在背景部分完成有限长度任务。
Something to clarify:
要澄清一下:
- Once your app is in the background and is frozen by the OS, there would be no way for your app by it self to wake up and re-connect to the internet.
- However, according to the article above from Apple, you can call this beginBackgroundTaskWithExpirationHandler method from your app's delegate to apply for additional time when put in the background, which is to say, though your app cannot wake up by it self when in background, it can, when in the background and not frozen, try to apply for additional time to finish its lengthy task.
一旦您的应用程序处于后台并被操作系统冻结,您的应用就无法自行唤醒并重新连接到互联网。
但是,根据Apple的上述文章,您可以从应用程序的委托中调用此beginBackgroundTaskWithExpirationHandler方法,以便在放入后台时申请额外的时间,也就是说,虽然您的应用程序在后台时无法自行唤醒,但可以,当在后台并且没有冻结时,尝试申请额外的时间来完成其冗长的任务。
Hope it helps.
希望能帮助到你。
#2
2
There is a trick that I think flayvr is using.
我认为flayvr正在使用一种技巧。
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.
他们使用显着的位置变化,当有人旅行一些显着的距离(大约500米)时,每个注册重要位置变化的应用程序将在有限的时间内被唤醒以执行一些快速任务,并将在几秒钟内终止。
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.
希望有所帮助。
#3
2
Until now you can do that on iOS7 with Background Fetch. Take a look at this article.
到目前为止,您可以使用Background Fetch在iOS7上执行此操作。看看这篇文章。
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.
但是,您只需要30秒即可完成任务。根据上面的文章,还有另一种称为后台传输服务的解决方案。
If more time is required though, then the Background Transfer Service API can be used
如果需要更多时间,则可以使用后台传输服务API
#4
1
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.
在Xcode中创建一个新项目,您将看到在app delegate文件中自动生成了大量新方法。像applicationDidEnterBackground,applicationWillEnterForeground等。
read the description you have to call your thread to upload data on server here.
阅读您必须在此处调用您的线程在服务器上上传数据的描述。
#1
13
I suggest you read this article from Apple carefully, especially the Completing a Finite Length Task in the Background section.
我建议你仔细阅读Apple的这篇文章,特别是在背景部分完成有限长度任务。
Something to clarify:
要澄清一下:
- Once your app is in the background and is frozen by the OS, there would be no way for your app by it self to wake up and re-connect to the internet.
- However, according to the article above from Apple, you can call this beginBackgroundTaskWithExpirationHandler method from your app's delegate to apply for additional time when put in the background, which is to say, though your app cannot wake up by it self when in background, it can, when in the background and not frozen, try to apply for additional time to finish its lengthy task.
一旦您的应用程序处于后台并被操作系统冻结,您的应用就无法自行唤醒并重新连接到互联网。
但是,根据Apple的上述文章,您可以从应用程序的委托中调用此beginBackgroundTaskWithExpirationHandler方法,以便在放入后台时申请额外的时间,也就是说,虽然您的应用程序在后台时无法自行唤醒,但可以,当在后台并且没有冻结时,尝试申请额外的时间来完成其冗长的任务。
Hope it helps.
希望能帮助到你。
#2
2
There is a trick that I think flayvr is using.
我认为flayvr正在使用一种技巧。
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.
他们使用显着的位置变化,当有人旅行一些显着的距离(大约500米)时,每个注册重要位置变化的应用程序将在有限的时间内被唤醒以执行一些快速任务,并将在几秒钟内终止。
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.
希望有所帮助。
#3
2
Until now you can do that on iOS7 with Background Fetch. Take a look at this article.
到目前为止,您可以使用Background Fetch在iOS7上执行此操作。看看这篇文章。
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.
但是,您只需要30秒即可完成任务。根据上面的文章,还有另一种称为后台传输服务的解决方案。
If more time is required though, then the Background Transfer Service API can be used
如果需要更多时间,则可以使用后台传输服务API
#4
1
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.
在Xcode中创建一个新项目,您将看到在app delegate文件中自动生成了大量新方法。像applicationDidEnterBackground,applicationWillEnterForeground等。
read the description you have to call your thread to upload data on server here.
阅读您必须在此处调用您的线程在服务器上上传数据的描述。