I'm making an app that contains a Today Extension. The today extension displays a list of timers, and if the user selects one of the timers, I'd like to create and schedule a local notification for that timer.
My problem is that scheduling of notifications is done with this line of code:
UIApplication.sharedApplication().scheduleLocalNotification(notification)
which very unfortunately relies on UIApplication.sharedApplication()
that is not accessible from an extension.
So my question is: How can I schedule a local notification from within a Today extension?
Funnily enough, I can make a framework with the code shared between my app and my extension and in that framework I can call:
func schedule() {
// ...
let settings = UIUserNotificationSettings(forTypes: .Sound | .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// ...
UIApplication.sharedApplication().scheduleLocalNotification(notification)
// ...
}
If I then import that framework from my extension and call schedule()
I get this output:
Failed to inherit CoreMedia permissions from 13636: (null)
Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7feaf3657a00>{fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, user info = (null)} with an alert but haven't received permission from the user to display alerts
Attempting to schedule a local notification <UIConcreteLocalNotification: 0x7feaf3657a00>{fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Saturday, November 1, 2014 at 1:01:48 PM Western European Standard Time, user info = (null)} with a sound but haven't received permission from the user to play sounds
So a module running in an extension has access to UIApplication.sharedApplication()
since this code actually tries to schedule a notification, but the system is not prepared for an extension asking for permissions to display alerts and so it fails (that's what it seems anyway).
How can I solve this?
Also, I know I can just launch my app using an url and schedule the notification from the app as normal, but that is not user firendly. The whole purpose of having the today extension is so that the user doesn't have to open the app to schedule the notification. The interaction has to be quick, simple and transparent and yanking the user out of the app they're in just to run a few lines of code and then be done with it is not how I want to do things.
See Question&Answers more detail:
os