5

I have a working music App, written in Swift. I wanted to integrate a sleep mode which could pause the music played after, say 10 minutes. I used perform(_ aSelector: Selector, with anArgument: Any?, afterDelay delay: TimeInterval) for this task, it worked fine.

But there's a problem. There's a good chance that the iPhone'll go to sleep before the 10 minutes end, so the pause fonction isn't called, and the music continues to play.

I understand that there are other ways to send an action after 10 minutes (NSTimer, use GCD) but if I understand, none of this solutions will prevent the machine to go to sleep, or wake up the app to perform the task.

Perhaps I could use background mode, but:

  1. My goal isn't really in the problems that this mode is supposed to solve. I don't want to have some time to finish, I just want a little 1/10 of second processor at a certain time.
  2. This option don't guarantee to work for a 10 minutes period.

Is this a way to achieve what I need?

FredericP
  • 1,119
  • 1
  • 14
  • 27
  • 1
    You could prevent the device from going to sleep with `UIApplication.shared.isIdleTimerDisabled = true`, but that would probably be throwing the baby out with the bath water. – Erik Apr 17 '17 at 12:59

1 Answers1

18

You can do: UIApplication.shared.isIdleTimerDisabled = true, as this is the only way in Swift 3, and you could do UIApplication.sharedApplication().idleTimerDisabled = truein Swift 2 or [UIApplication sharedApplication].idleTimerDisabled = YES; in objective-c

  • 1
    This is working, but I have two problems : first this will keep the display on, and that was not mandatory for my purpose. But more important : Is Apple going to validate an app with this setting? (even if I disable it as soon as the app pauses the sound) Others means to obtain the same type of results are allowed only for specific type of Apps by Apple. – FredericP Apr 17 '17 at 14:24
  • If that isn't what you need, then I am sorry; I do not know otherwise. And yes, Apple would allow it: Ive seen loads of apps with that feature- sorry I wasn't really that helpful. –  Apr 19 '17 at 16:43