1

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!

Ron
  • 1,047
  • 13
  • 18
  • Wouldn't you need to use backgroundSessionConfiguration to create a task that will complete in the background? – Matt Gibson Aug 14 '14 at 14:56
  • Hi, also tried with: var defaultConfigObject = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("identifier") but same issue – Ron Aug 14 '14 at 15:37
  • Did you enable Background Modes in the capabilities? https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html – Poulet numéro 1687547878234 Aug 08 '16 at 12:16

0 Answers0