2

Let's assume I have an app that has the idle timer disabled and will continue to run when the application state is not active (in background while using other apps or in lock screen, think audio playback) but it should stop when the device enters sleep mode (i.e. the screen goes black).

Is there a way to detect that event and stop the app's processes?

Since it would be an app utilising the audio playback required background mode it would continue otherwise.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
nontomatic
  • 2,003
  • 2
  • 24
  • 38
  • Well, if I understand all that background stuff right: If you manage to keep your app alive in the background, (I mean: longer than the "allowed" 5 to 7 seconds), than there will be no sleep mode. Your app will prevent it. – Hardy_Germany Nov 03 '18 at 20:40
  • The app is allowed to continue in the background during lock & even sleep due to its "required background mode". These can be set in the app's plist. But this setting will not stop the device from entering lock & sleep. Setting the idleTimerDisabled property can do that but you can only set it for your app, so if the user switches to another app, your app is exposed to dealing with lock etc. again. – nontomatic Nov 03 '18 at 21:23
  • Maybe I missunderstand your question, because of the example you mentioned with "playing audio in background". If you play audio in background, the device will not fall asleep. The playing keeps it awake. With "require background mode" you usually get 180 sec. of background time – Hardy_Germany Nov 03 '18 at 21:51
  • Maybe I missunderstand your question, because of the example you mentioned with "playing audio in background". If you play audio in background, the device will not fall asleep. The playing keeps it awake. With "require background mode" you usually will get 180 sec of background time to do what you have to do... (finish download, handling location updates etc.) ... but again, if I understand the mechanism right, if your app works in the background, the device will not fall asleep. If its falls finally asleep, your app does not "run" in the background. It will be suspended and with no CPU time. – Hardy_Germany Nov 03 '18 at 21:57
  • Just so we're on the same page, in my understanding sleep mode is what you get when pressing the power button quickly so that the screen goes black. Not holding to power off. Can you confirm that? There is a distinction between sleep and lock screen. If what you wrote were true, I wouldn't have been able to listen to Spotify in sleep mode the previous 15 mins. – nontomatic Nov 03 '18 at 22:37
  • Well you can listen to an audiobook for several hours. The screen is than off, but the device does not sleep. Sleep means, yonly a few very basic IOS functions are alive, Some kind of "stand by". the battery will last up to two days in that state. In that state no enduser app has any CPU time available. – Hardy_Germany Nov 03 '18 at 23:07
  • I don't think from a developer's perspective there's any difference between the two types of state you've described. Developers can ask the device to keep their app going in background/sleep, and it will run until the device is out of battery. So, as mentioned in the OP, I'm talking about that moment, when the screen goes black. I'd like my app to react to that moment and stop all processes (it won't stop otherwise). – nontomatic Nov 03 '18 at 23:12
  • An app can be up and running in foreground or in background. You get 4 to 6 seconds background time if you do not claim background availability (plist), or up to 180 sec. In background your app can also be suspended (app is "freezed", but could continue instantly, it simply gets no CPU time at all) or terminated (thrown out of the memory, has to be reloaded and restated to continue)... IOS is a real "diva". If you are not in foreground, it gives you CPU time or not, just as it thinks it will be the best for all, at a given time on the device. – Hardy_Germany Nov 03 '18 at 23:16
  • No, you can claim background time, but it will not be guaranteed, you even could not be sure that your 180 sec are a continuous, uninterrupted line of time ;-) test it, you will be surprised, I spent very frustrating months to finally understand the basic behaviour – Hardy_Germany Nov 03 '18 at 23:17
  • Thank you for the replies, but I don't think your focus is on answering the question. I gave all information: I want to keep my app running while other apps are active but I want to kill it, once the screen goes black. I know how to do the first part. – nontomatic Nov 03 '18 at 23:20
  • look at this: might help ... https://stackoverflow.com/questions/48293420/how-to-detect-screen-lock-unlock-in-ios-swift-4 – Hardy_Germany Nov 03 '18 at 23:24
  • thanks, I will have a look. If I happen to find the answer to my question and nobody else did in the meantime, I'll post it here. What I've found so far and what makes it difficult to get useful information is that most people don't seem to understand that there is a distinction in the resources offered to an app in background between the iPhone being in lock screen and black screen. – nontomatic Nov 03 '18 at 23:34

0 Answers0