Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

URLSession Tutorial: Getting Started

In this URLSession tutorial, you'll learn how to create HTTP requests, as well as implement background downloads that can be both paused and resumed.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/567-urlsession-tutorial-getting-started

Should the starter project work on earlier versions of Xcode and Swift? Because I’m getting the following error on Xcode version 8.2.1.

“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

And when I try to convert to current Swift syntax Xcode doesn’t detect any playground to convert.

hi! here are projects that work with Xcode 8.3.2

HalfTunes Xcode8.3.2.zip (168.7 KB)

It’s mainly the settings (change target version to 10.3, Swift version to 3) and replacing the storyboard with one that doesn’t use safe area layout guides; plus a line of code in AppDelegate. But all the code you add in the tutorial should work in Xcode 8. The attached projects run in Xcode 8.3.2.

2 Likes

Thank you so much. I really need the starter project to complete this tutorial. I am really interested in using URLSession :smiley:

1 Like

Hi, the Starter project needs a small update to compile with Xcode 9 beta 4.

Have a nice day.

thanks! I’ll do a quick check when they release GM :]

Hi, PFA, I am getting this error in xcode 8.2.1 , please suggest how can I resolve this.

Regards,
Vishal

delete StoryboardSupport.swift

in SearchViewController.swift, in tableView(_:cellForRowAt:), replace the first line with this:

let cell: TrackCell = tableView.dequeueReusableCell(withIdentifier: "TrackCell", for: indexPath) as! TrackCell

Kudos :slight_smile:

Thanks Its Working Now !

Hi Audrey!

Great tutorial, really feels years of academic background and teaching. Very clear and useful!

By the way, seems there’s a small bug in the last part. You try to call UIApplication.delegate in the background thread. So, to prevent this, you can wrap the whole if-statment to call it in the main thread:

DispatchQueue.main.async { // Because of UIApplication.delegate calling
  
  if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
     let completionHandler = appDelegate.backgroundSessionCompletionHandler {
    appDelegate.backgroundSessionCompletionHandler = nil
    completionHandler()
  }
  
}

thanks Alexey! I’ve updated the text and final project now.

@nilocski: I fixed that NSAttributedStringKey syntax, too, in both starter and final projects. Thanks again!

Great tutorial, but you glossed over one section in which I could use some clarification. From Apple’s documentation: “Your launch code should recreate the session, using the same identifier as before, to allow the system to reassociate the background download task with your session”. In your tutorial there is no need, but how does one do this?

Thank you, this nice post but how about implement in Obective-C?

there’s an old Obj-C tutorial at https://www.raywenderlich.com/67081/cookbook-using-nsurlsession

but this site has been primarily-Swift since Apple announced it in 2014.

Hi, thank you for the tutorial. It is very helpful. I am new to IOS development and I am trying to build an app that reuses some web components. It will be great if you can write some deep tutorials on webview, WKWebView, and how JavasScript and Swift communicate.

have you seen https://www.raywenderlich.com/124075/javascriptcore-tutorial ?

Oh great thank you. I will have a look!!

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]