NSURLSession Tutorial: Getting Started

Nice Tutorial. It helps me to get more idea on Url session.

Background related code does not get called. I ran the final version and the background completion code in the app delegate and view controller do not get called. Functionality seems to work fine(downloads complete). I’m using IOS 9.3. Wondering if this has something to do with IOS version.

Thanx

jra

For everyone who is interested in a working solution in Swift 3.0. Checkout my repository: GitHub - ronatory/nsurlsession: An app which lets you query the iTunes Search AP and download 30-second previews of selected songs. Used the Tutorial from https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started as basic. Updated to Swift 3.0 . Date of creation: Nov 08, 2016

Hi,
It might be a bit late but here is the solution I found and worked for me on SWIFT 3.0
https://codedump.io/share/uTLiWZKM79mY/1/resume-nsurlsession-on-ios10

thanks,
Amir

Hi ashramsey,
I am having the same issue, with internet disconnectivity it triggers didCompleteWithError delegate and with no resume data so suppose you have 80% of file downloaded and on net falure it would loss all data and restart again. Any solution on this? is this Bug BTW?

Thanks,
Amir

Strange situation I’ve encountered. I spent a day going through the tutorial, converting it to Swift 3 as I do (it helps solidify the concepts). It worked perfectly fine in the simulator, but not on an iPhone 6s running the most current iOS. The player plays, but no audio comes out (and yes, the volume is up :-)).

I then updated the deployment target to 10.2 and updated playDownload to this:

func playDownload(_ track: Track) {
    if let urlString = track.previewUrl, let url = localFilePathForUrl(urlString) {
        let player = AVPlayer(url: url)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player?.play()
        }
    }
}

Same problem. Any ideas?

Did you find a solution ?
I have same problem when I go to another fire controller and then return to same view controller
I got this error “A background URLSession with identifier backgroundSession already exists!”
and the progress view won’t updating after that !

I’ve created my custom DownloadSession class and one instance of it in the AppDelegate. If you have the URLSession instance in a ViewController class, every time you create (navigate to) that view controller it creates an URLSession instance again with the same identifier and that fires the error (this was my case).
I’m busy but let me know if you need some code, I hope it helps.

I updated this app for my update to the URLSession video course. IIRC the existing music player code was deprecated, so I changed it to use AVKit and AVFoundation:

import AVKit
import AVFoundation
// ...
func playDownload(_ track: Track) {
  let playerViewController = AVPlayerViewController()
  present(playerViewController, animated: true, completion: nil)
  let url = localFilePath(for: track.previewURL)
  let player = AVPlayer(url: url)
  playerViewController.player = player
  player.play()
}

but I haven’t installed it on a device — let me know if it works for you?

hi: the background session identifier is only used by the system, if it relaunches your app by calling AppDelegate’s handleEventsForBackgroundURLSession identifier

didWriteData is a session delegate method, and you can set the session delegate to another class when you create the URLSession — you don’t have to set it to self

you don’t need to change URLSession, but you should use a download task if the PDF is large, to save it as a file instead of in memory

I think you should use the session cache for this. The server should specify in its response that the contents must be revalidated, and your app caches the response. See Cache Use Semantics for the HTTP Protocol.

I’ll try to cover this when I update this tutorial.