0

Apple resources contain a lot of informations but there's one thing which I can't clearly understand reading about audio and notification.

Is it possible to make an app, running in background which produce sound (even if phone is locked and/or silenced) and when it's happend user must solve eg. equation to turn it off?

p.s. For now I mostly use Cordova framework but Obj-C tip will also be nice.

kiler129
  • 1,063
  • 2
  • 11
  • 21

2 Answers2

1

Yes it is posssible. You can use UILocalNotification for this.

Also apple allows apps that are playing music in background.

Please check these links for the background task feature:

  1. ManagingYourApplicationsFlow
  2. ios multitasking background tasks
  3. How to handle background audio playing while ios device is locked or on another
Community
  • 1
  • 1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 1
    UILocalNotification isn't a solution I think (sound is limited to 30 seconds). I also know about background music playing but my question is can I start it at specified time while app's in background? If it's possible to start music from background at desired time I can combine it with notification (music starts, notify pop-up and direct user to app where music can be turned off after answering question). – kiler129 Jan 11 '13 at 04:29
  • 1
    @kiler129:I think you can use the background task feature for doing this. – Midhun MP Jan 11 '13 at 04:33
  • After reading a ton of SO posts and Apple docs I think it's nearly impossible to make reliable alarm clock which run in background. – kiler129 Jan 12 '13 at 04:52
0

You can change Local Notifications for NSTimers (keeping them alive in inactive mode with https://github.com/mruegenberg/MMPDeepSleepPreventer) and calculate the time interval for each alarm. That way you can then play an audio even with the screen locked and the sound off pasting this in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

// Let the sound run with the screen blocked
NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

But you will have some problems:

  • The app must be playing an audio file each 10 seconds so it doesn´t deep sleep and kills all NSTimers.
  • Apple could reject your app for doing so.
  • You can´t close the app with the home button, otherwise, it won´t work.
  • You must open the app every time you need to use the alarm (you can´t schedule and forget).
  • When the alarm fires, you only have the lock screen of the iPhone and need to unlock it first and then stop the alarm from inside the app.

In Apple they don´t want competitors for their alarm clock app, that's for sure! Almost all the alarm clock apps you see in the App Store use this poor approach.

Ruben Marin
  • 1,639
  • 14
  • 24