Search code examples
iphoneandroidserviceios4

Android has Services, but is there an equivalent or way to implement similar functionality in iOS?


In my Android app I can easily create a Service object that can run in the background to perform arbitrary routines. In my case I have a Service which maintains a connection with a server and upon receiving messages it notifies the user. This service is only turned on when a particular activity enters its onPause or onStop cycle and is turned off when the activity is resumed

Is there some way to do this with iOS? I've poked around and only have come across this page on the Apple developer site:

http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Everything described there doesn't seem to satisfy my needs so I wanted to check with the SO community to see if anyone has some sort of solution.


Solution

  • The Apple way to do what you are talking about is to use the Push Notification Service. Generally speaking in 4.x+ you have Remote Notifications and Local Notifications. You don't have to go direct, Urban Airship offers a web service option if you don't like hacking raw TCP streams.

    Also, if the push service is just not compatible with your architecture (a twitter client for example) when your app quits you can request more time for background execution to finish downloads / schedule more work with the APIs you found at that URL.

    It's kind of cheating, but you can register your app for location updates using the UIBackgroundModes Info.plist property and when you get a location update, "just so happen to also check for new messages" at that time as well. In effect this is piggie-backing your message polling code on the location change event.