I have an iPhone application like facebook for iPhone. My application must connect my server and read all message every two hours regularly. I have a thread to read all message but when the application is terminated the thread cannot work. Can the thread run undependently from main delegate or how can I find solution for this problem?
2 Answers
You cannot have your app do stuff in the background. There is an API to finish tasks like uploading a photo but even that will be killed after around 10 minutes.
But the Apple Push Notification Service seems like the most appropriate solution for your problem. Your server notifies the device that there is something new happening and you fetch the actual messages when the user opens the app.
edit: As of iOS 7 Apple implemented a feature where you can schedule running tasks to fetch data in the background. Those tasks are not guaranteed to run at any specific times. See the release notes for iOS 7 and the linked methods below:
Apps that regularly update their content by contacting a server can register with the system and be launched periodically to retrieve that content in the background. To register, include the UIBackgroundModes key with the fetch value in your app’s Info.plist file. Then, when your app is launched, call the setMinimumBackgroundFetchInterval: method to determine how often it receives update messages. Finally, you must also implement the application:performFetchWithCompletionHandler: method in your app delegate.

- 6,702
- 1
- 22
- 25
-
I was going to write the same, but @klaustopher was first. You can watch WWDC videos or documentation if you have iOS Developer subscription. They explain it there very clearly. – Ahmed Al Hafoudh Jun 04 '12 at 13:09
-
i tried NSNotification but if there isn't new message to specified time, NSNotification doesn't run one more times. Is there any sample code your arshive and can you share please. – SerkanHocam Jun 04 '12 at 13:38
-
There are plenty of examples how to implement the push notidication service. If you also write the server code, just google for "apple push notification service {your language}". If you don't have access to the server you need to create some sort of proxy that runs regularly on a server, pulls information from the original server and pushes the information to the users iPhone – klaustopher Jun 04 '12 at 13:52
-
" There is an API to finish tasks like uploading a photo but even that will be killed after 30 seconds." If you're taking about shouldBeginBackgroundTaskWithExpirationHandler that permits about 10 minutes of execution. If you're not talking about that, then which API do you mean? – Gruntcakes Jun 04 '12 at 14:57
-
True, I changed my answer. I remember in one of the WWDC sessions they mentioned the 30 seconds and since I never used it, I didn't follow up on that number. But the documentation doesn't mention any explicit time frame, so this might be subject to change. – klaustopher Jun 04 '12 at 15:10
-
Apple doesn't mention a figure, in practice it is 10 minutes, but as you say this amount should not be counted on as it might change, but there is actually no guarantee anyway that you will get 10 minutes or 30 seconds or anything. – Gruntcakes Jun 04 '12 at 15:17
-
i will try to write this system's server side and iphone side. if i success i share my code block. – SerkanHocam Jun 05 '12 at 06:13
-
i m still find solition my problem. i can not get rss regularly from a rss resource when my application is closed. i have begun to hate ios unfortunatly. :( – SerkanHocam Jun 11 '12 at 12:36
-
Yes i tried finally this solition. but easy apns doesn't work. i found another way. thank you everyone. – SerkanHocam Nov 12 '12 at 14:32
There is no solution.
Apple does not permit applications to run in the background unless they are of a specific type such as location or audio or voip or newstand (your app can continue to run for about 10 minutes after it was active if it uses shouldBeginBackgroundTaskWithExpirationHandler).
There is no workaround, many many other people have wondered how to do the same thing as yourself before, but there is no legitimate way. Your app cannot schedule any sort of periodic call home activity.
The only way your app can run once its gone into a suspended or terminated state is for the user to launch it, either explicitly or in reponse to a local notification or remote push notification.

- 37,738
- 44
- 184
- 378