0

I've seen this come up a few times but I haven't really seen a definite answer...

Since iOS 5 my application seems to run into the problem that if the user locks the screen my NSTimer stops firing after about 20 seconds in the lock screen. This makes my app which plays a sound every time the timer fires pointless if the user locks the screen.

This has only happened since I updated my device to iOS 5.

My question is that 1. Is there any workaround to having this same functionality work on updated devices or 2. What is the recommended approach to not receiving bad reviews if your app doesn't run while locked.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
Eric
  • 5,671
  • 5
  • 31
  • 42
  • have you tried ipad? i found the timer event would be received on my iPad 2 iOS 6.0, but won't work on my iPod touch 4 iOS 6.0. – CarmeloS Nov 07 '12 at 08:09
  • I did not try on iPad, but had it working from the selected answer below on iPhone – Eric Nov 07 '12 at 20:16

2 Answers2

1

In iOS5, Apple has introduced more background task modes, including "audio", "location", "voip", "newsstand-content", "external-accessory" and "bluetooth-central". I think you can determine which mode you can use in your app. The link is http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW24

mxi1
  • 166
  • 7
1

When the user locks the screen, the app enters the 'inactive' state. This is similar to but not identical toputting your app into the background. Apple's documentation states "The app is running in the foreground but is currently not receiving events. (It may be executing other code though.)"

However, after entering the inactive state the iPhone does go completely to sleep, unless something is running to keep it active... waiting for an NSTimer to fire does not satisfy this criteria, apparently. There is discussion in this previous question. One solution, it would appear, is to play your sound when your timer fires, and in the interim, play a silent sound continuously in order to prevent the iPhone from sleeping. A hack, but at least in this case not a completely ugly hack since using a sound in order to keep the iPhone awake in order to play a sound has a kind of symmetry.

Previously linked content:
From Apple's guidelines regarding implementing long-running background tasks:

Playing Background Audio
An app that plays audio continuously (even while the app is running in the background) can register as a background audio app by including the UIBackgroundModes key (with the value audio) in its Info.plist file. Apps that include this key must play audible content to the user while in the background.

Are you implementing this in your app? If not, that's your issue.

Regarding #2: You could update the description of the app to indicate it does not work with the screen locked, but better would be just to make it so that it works as the user would expect. :)

Community
  • 1
  • 1
Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
  • My question is that does adding this to the plist allow for NSTimers or just the continuation of a playing audio sound – Eric Nov 20 '11 at 02:53
  • 1
    You make an excellent point, and that and some other reading led me to revise my answer with a possible solution. I've left the previously linked content there for future reference. – Duncan Babbage Nov 20 '11 at 03:27
  • haha thats exactly what I was going to attempt once I got back to my computer... I figured as long as I kept something happening in the background during the time between sounds, I could avoid the deep sleep. Ill try this and approve the answer as soon as it works ;) – Eric Nov 20 '11 at 06:39
  • So I added the silent sound when the user app goes into the background... only thing now is that it will keep playing the sound no matter what app your in... Before (in past iOS versions) I had it so that the app would only play when locked, not when the home button was pressed. Seems pretty confusing to have a random sound playing while you are in other apps unless its a music player.. Don't know how to get this type of functionality – Eric Nov 20 '11 at 10:20
  • Since the lock screen doesn't activate applicationDidEnterBackground, you can either turn the sound off there, or set the flag that indicates the app should exit when it is put into the background. – Duncan Babbage Nov 20 '11 at 18:51
  • Application state when locking the screen is something that changed in iOS 5. See the discussion about the differences between iOS 4.3 and iOS 5 at http://stackoverflow.com/questions/7754317/is-it-possible-to-distinguish-between-locking-the-device-and-sending-an-app-to-b – Andrew Hershberger Jun 03 '12 at 03:05