# NSURLSession Tutorial: Getting Started

**URL:** <https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047>\
**Category:** 🎓 Courses & Articles\
**Created:** [February 28, 2016, 8:21pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047 "2016-02-28T20:21:40Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![jiten](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jiten/32/81494_2.png) [@jiten](https://forums.kodeco.com/u/jiten)\
**Post date:** [October 21, 2016, 6:36am UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/21 "2016-10-21T06:36:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jraldrin](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jraldrin/32/81494_2.png) [@jraldrin](https://forums.kodeco.com/u/jraldrin)\
**Post date:** [October 25, 2016, 6:21pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/22 "2016-10-25T18:21:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ronwp2](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ronwp2/32/46856_2.png) [@ronwp2](https://forums.kodeco.com/u/ronwp2)\
**Post date:** [November 8, 2016, 4:15pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/23 "2016-11-08T16:15:22Z")

</div>

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](https://github.com/ronatory/nsurlsession) . Date of creation: Nov 08, 2016

---

<div class="post-metadata">

**Author:** ![amirpervaiz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amirpervaiz/32/102221_2.png) [@amirpervaiz](https://forums.kodeco.com/u/amirpervaiz)\
**Post date:** [December 3, 2016, 5:42pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/24 "2016-12-03T17:42:30Z")

</div>

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](https://codedump.io/share/uTLiWZKM79mY/1/resume-nsurlsession-on-ios10)

thanks,  
Amir

---

<div class="post-metadata">

**Author:** ![amirpervaiz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amirpervaiz/32/102221_2.png) [@amirpervaiz](https://forums.kodeco.com/u/amirpervaiz)\
**Post date:** [December 3, 2016, 5:45pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/25 "2016-12-03T17:45:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![azcoder](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/azcoder/32/69717_2.png) [@azcoder](https://forums.kodeco.com/u/azcoder)\
**Post date:** [February 9, 2017, 5:59pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/26 "2017-02-09T17:59:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![x901](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/x901/32/110294_2.png) [@x901](https://forums.kodeco.com/u/x901)\
**Post date:** [February 15, 2017, 11:41pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/27 "2017-02-15T23:41:43Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![cubick84](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cubick84/32/57705_2.png) [@cubick84](https://forums.kodeco.com/u/cubick84)\
**Post date:** [February 16, 2017, 8:20pm UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/28 "2017-02-16T20:20:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [April 5, 2017, 12:28am UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/29 "2017-04-05T00:28:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [April 5, 2017, 1:05am UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/30 "2017-04-05T01:05:58Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [April 5, 2017, 1:08am UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/31 "2017-04-05T01:08:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [April 5, 2017, 3:16am UTC](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047/32 "2017-04-05T03:16:45Z")

</div>

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](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Concepts/CachePolicies.html#//apple_ref/doc/uid/20001843-157113).

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

[Previous page](https://forums.kodeco.com/t/nsurlsession-tutorial-getting-started/1047.md?page=1)
