1

I have a piece of code wherein I make a server call and based on the response I play a sound. Now, this does not work when my application is in background or my devicei is locked.

Is there any way we can execute this piece of code (Server call and response handling) even if app is in background or device is locked?

Abhinav
  • 37,684
  • 43
  • 191
  • 309

1 Answers1

5

There is no general solution, which is by design. (Apple does not want you to have a potentially CPU- and power-intensive process running in the background and degrading user experience.)

There are a few limited-case options available:

  1. You said you want to play a sound. If by this you mean "play music" or some such streaming, there is an audio background task that your application can register to perform. Note that you must actually be streaming audio; Apple rightfully frowns upon apps that try to use this approach to circumvent the general-case prohibition and will reject your App Store submission accordingly.
  2. You can invert your scenario and have the server send a push notification through the Apple Push Notification service. Depending on the user's settings, an alert, badge, or sound can result. This might be the best fit for you if you aren't streaming audio.
  3. If what you are really intending to do is, say, finish an upload or download (and the sound is a completion notification), you can request some additional time to finish that task after the app is nominally backgrounded or the device locked. Use -[UIApplication beginBackgroundTaskWithExpirationHandler:] from within the appropriate UIApplicationDelegate methods to register such a task. Note that you have a limited (but appreciable) amount of time to finish your task, and I don't think you can play media in this mode.
Conrad Shultz
  • 8,748
  • 2
  • 31
  • 33
  • So, what I am looking for is even when application is in background/locked I could make a server call to get the latest data and then based on the data I could play a WAV file in my bundle. Is there any way to do that. #2 and #3 does not seem to be a viable solution here. – Abhinav Jul 03 '12 at 03:08
  • @Abhinav No, you cannot do that. There is no support for general purpose background applications. Can you perhaps explain *why* you are trying to do this? – Conrad Shultz Jul 03 '12 at 03:14
  • Ok. Thanks for explaining this. Well, as I said I wanted to run my piece of code even when app is in background. Guess we cannot do that the way it works for iPod app. – Abhinav Jul 03 '12 at 22:42
  • It is possible, a lot of alarms do this but I dont know how. [I have a thread on this too](http://stackoverflow.com/questions/9725192/how-do-i-start-playing-audio-when-in-silent-mode-locked-in-ios-6). – Andres Canella Oct 08 '12 at 03:02
  • @AndresCanella Again, that's not a *general* approach. Take a look at local notifications, http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html, for how that kind of action can be accomplished. – Conrad Shultz Oct 08 '12 at 18:59
  • @ConradShultz Local notifications will not run code unless you press the action button. If I am wrong please provide some code that will run as the local notification fires. Local notifications do not sound if the iphone is set on silent, yet if you try the ihandy alarm app. you will find that you can lock the screen, set the phone to silent and the alarms will still sound. Visit my thread I mention, we've already discusses Local notifications extensively. I and a lot of pople would appreciate it immensely if you provide how to replicate this functionality. – Andres Canella Oct 09 '12 at 20:15