I'm trying to implement a NSURLSessionDownloadTask with the following code in swift and iOS 8:
var dlURL = NSURL(string: http://thedownloadurl")
var defaultConfigObject = NSURLSessionConfiguration.defaultSessionConfiguration()
defaultConfigObject.timeoutIntervalForRequest = 300.0
defaultConfigObject.allowsCellularAccess = true
var defaultSession = NSURLSession(configuration: defaultConfigObject, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
var downloadTask = defaultSession.downloadTaskWithURL(dlURL)
downloadTask.resume()
The I capture the response with:
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) {
println("Downloaded to location: \(location)")
let str = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
println(str)
var localNotify = UILocalNotification()
localNotify.alertBody = "Download completed"
localNotify.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().presentLocalNotificationNow(localNotify)
}
When I run this is the simulator, all works as expected and it finishes the download with a pretty notification showing up onscreen, even if the device is put to lock screen. However, when I test it on my iPhone 4s, the download will only only complete once I put the app back into foreground. Any ideas as to why this may be?
EDIT: For completeness, I also tried with
var defaultConfigObject = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("uniqueIdentifier")
But still the download only finishes when the app is put to foreground.
Thanks!