kiler129
kiler129

Reputation: 1154

Alarm clock with answer-to-disable on iOS

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.

Upvotes: 0

Views: 627

Answers (2)

Ruben Marin
Ruben Marin

Reputation: 1637

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.

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107201

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

Upvotes: 1

Related Questions