Reputation: 415
I am making an app, that is reliant on executing some minor code in the background and I ran into a peculiar problem.
The app has a timer, an NSTimer()
. The principle behind all of this is similiar to that of timer (in Clock application, that is installed on all iOS devices), meaning, when the timer ends a UILocalNotification
is displayed.
Everything works as expected when I run it on an emulated device in Xcode, but when I test it on my iPhone, there are no notifications. It is something along the lines of:
var end = timerHasEnded()
if end == true{
println("the timer has ended")
}
and it was not working. So I checked if the application even detects the background UIApplicationState
by doing this and it does not on the physical device. Interestingly enough, it does so on an emulated device.
I tried running the timer on background thread, using QOS_CLASS_BACKGROUND
and dispatch_async
to no avail. Can anybody help me?
Upvotes: 0
Views: 314
Reputation: 1754
Could you just schedule a UILocalNotification to appear after a delay, when the delay was the remaining time left in your alarm?
var date: NSDate = /*time until your timer expires*/
let app = UIApplication.sharedApplication()
let oldNotifications = app.scheduledLocalNotifications
if oldNotifications.count > 0 {
app.cancelAllLocalNotifications()
}
let alarm = UILocalNotification()
alarm.fireDate = date
alarm.timeZone = NSTimeZone.defaultTimeZone()
alarm.repeatInterval = 0
alarm.soundName = "myAlarmSound.caf"
alarm.alertBody = "Do something!"
app.scheduleLocalNotification(alarm)
I wrote this code from an Obj-C example provided by Apple.
Upvotes: 1