iosswiftbackgrounduibackgroundtask

iOS App running background task time


So I'm performing a background task in my app, which is started like this:

private func someFunc() 
{
    backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
        // Present a local Notification
    })

    // Present a different local Notification

    DDLogInfo("Is in bg: \(UIApplication.sharedApplication().applicationState == .Background)")
    DDLogInfo("BG-time remaining: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
}

My problem is that backgroundTimeRemaining always returns 1.79769313486232e+308. This should only happen when the app is active. But this function is only called when the app is running in the background, the first log statement confirms this.

I also tried to dispatch the log statement after 5 seconds and disconnecting the device from Xcode (as someone on another SO post suggested), but without success.


Solution

  • As it turns out, this was a bug in iOS 9.1 and has been fixed since 9.2. If you still encounter this problem, make sure your app is actually running in the background :)