Search code examples
iosobjective-ciphonebackground-process

How to schedule an alert which wakes up iOS app without user intervention


My goal: implement sleep function in my music audio player. If user chooses say 30mins sleep time then the app should wake up and stop the currently played music.

Problem: [self performSelector:@selector (onTimer_sleep) withObject:nil afterDelay:sleepIntervalInSeconds]; does not work once the phone is locked;

My try: use UILocalNotification. This however works only if the app is foremost and visible when the system delivers the notification.

What would you recommend for my use case? Should I implement background mode? Which bg mode would you suggest, "Background fetch"?


Solution

  • Short answer: You can't. Apple severely limits the apps that are allowed to run in the background.

    If your app is a music playing app that supports playing from the background then you are all set. That is one of the small list of apps that are allowed to run in the background for more than a couple of minutes. While music is playing in your app you should still get background time.

    As you've discovered, local notifications only get delivered if the user taps on them, or if the app is running in the foreground.

    You could set up a server to send silent push notifications, but that would require a network connection, and push notifications can't be timed very precisely.