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?

1
  • 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
    Commented Apr 17, 2017 at 12:59

1 Answer 1

19

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

2
  • 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
    Commented Apr 17, 2017 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.
    – user7258317
    Commented Apr 19, 2017 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.