Questions tagged [urlsession]

URLSession is the API for HTTP connections introduced in iOS 7 and OS X 10.9.

The class URLSession was introduced to Apple's Foundation.framework in iOS 7 and OS X 10.9.

A URLSession object can be considered a group of related data transfer tasks. A response may be received and can be handled via a block statement or delegate methods. URLSession provides status and progress properties, of which support canceling, resuming, or suspending tasks, and resuming suspended, canceled, or failed downloads.


Types of Sessions

URLSession can support three different types of sessions, which is determined by the configuration object used to initialize the session object.

  • Default Sessions
  • Ephemeral Sessions
  • Background Sessions

Types of Tasks

There are three types of tasks, the last two (Download & Upload) of which support background use.

  • Data Tasks - Send and receive data via a NSData object
  • Download Tasks - Retrieves data in the form of a file
  • Upload Tasks - Sends data, most typically in the form of a file

Related tags to explore:

Adapted from Apple's URL Loading System Programming Guide > NSURLSession.

785 questions
7
votes
2 answers

URLSession.dataTask is timing out on every request with Xcode 12.5 Beta 2

I upgraded to the latest Xcode 12.5 Beta 2 today, and now all of my URLSession.dataTask requests are failing and timing out. I create a sample project that makes a simple request, but it's failing each time. It works find with Xcode 12.5 Beta…
lepolt
  • 501
  • 5
  • 13
7
votes
2 answers

Using URLSession to load JSON data for SwiftUI Views

What are proven approaches for structuring the networking layer of a SwiftUI app? Specifically, how do you structure using URLSession to load JSON data to be displayed in SwiftUI Views and handling all the different states that can occur properly?
Ralf Ebert
  • 3,556
  • 3
  • 29
  • 43
7
votes
1 answer

URLSession with Server-Sent Events sometimes return kCFErrorDomainCFNetwork 303

In our App we request a stream of information with Server-Sent Events. For this we use the small Library IKEventSource Which under the hood uses Foundation.URLSession. This Information is sent in small JSON Packages For…
Peter Schumacher
  • 441
  • 4
  • 19
7
votes
2 answers

How to sync serial queue for URLSession tasks?

Using XCode-8.2.1, Swift-3.0.2 and iOS-10.2.1, I am trying to call two different URLSession.shared.dataTasks (the first is a simple URL-request and the second is a POST-request). Since my first dataTask delivers a result that is needed in the…
6
votes
0 answers

Why does URLSession.shared.data fail and complain about an unconnected nw_connection?

I'm pretty new to coding in Swift and am currently working on my very first iOS app. Part of my app has to talk to the Spotify API by making requests and parsing JSON that it returns. I have a relatively simple async function that I use to achieve…
kawub
  • 113
  • 1
  • 5
6
votes
0 answers

Unit testing for URLSessionDelegate method - Swift

I have a custom delegate class called CertificatePinningDelegate which conforms to URLSessionDelegate. I'm using the delegate method urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping…
6
votes
1 answer

How to resolve - Combine Error | Error Domain=NSURLErrorDomain Code=-999 "cancelled"

I am attempting to parse JSON using the new Combine framework. However, every attempt I make comes back with a cancelled error. When I use the exact same url without combine it works fine. The bottom function works fine, the top one gives me an…
Michael Wells
  • 586
  • 6
  • 14
6
votes
2 answers

How to stub URLSession in Swift?

I have been following this tutorial to stub out URLSession. The example was done by creating a protocol and extending the existing URLSession. protocol URLSessionProtocol { typealias DataTaskResult = (Data?, URLResponse?, Error?) -> Void …
Houman
  • 64,245
  • 87
  • 278
  • 460
6
votes
0 answers

URLSessionConfiguration's httpMaximumConnectionsPerHost not working in iOS 10?

Setting this parameter doesn't affect the number of simultaneous connections to a host. I've found the same issue on apple developer forum. It looks like a bug in iOS 10, iOS 9 works properly. let sessionConfiguration =…
6
votes
3 answers

Is NSURLCache thread safe?

I know NSCache is thread safe, however I can't find out any document mentioned that NSURLCache is thread safe.
Pei
  • 460
  • 4
  • 21
5
votes
1 answer

Handle URLSession timeout

I am use URLSession for POST request and obtain list of players, but sometimes I am obtain error "Error Domain=NSURLErrorDomain Code=-1001", how I can handle it? How I am get error inside "completion"? func addPlayer(playerCode: String, completion:…
Ice
  • 680
  • 1
  • 10
  • 23
5
votes
1 answer

URLSession.shared.dataTask vs dataTaskPublisher, when to use which?

I recently encounter two data fetching (download) API that performs seemingly the same thing to me. I cannot see when should I use one over the other. I can use URLSession.shared.dataTask var tasks: [URLSessionDataTask] = [] func…
Elye
  • 53,639
  • 54
  • 212
  • 474
5
votes
1 answer

Swift Combine URLSession retrieving Dataset/Photos using 2x Publishers

I have majority of the functionality working and returning exactly what I want. However, I'm having bit of a brain fart when it comes to taking the photos array in the response and assigning them to appropriate employees to be able to render them.…
Paul D.
  • 156
  • 2
  • 12
5
votes
2 answers

How can I pass a dependancy to a URLProtocol subclass?

I have been working on an interceptor for adding auth headers to network requests in my app. final class AuthInterceptor: URLProtocol { private var token: String = "my.access.token" private var dataTask: URLSessionTask? private…
Harry Blue
  • 4,202
  • 10
  • 39
  • 78
5
votes
3 answers

How to mock URLSession.DataTaskPublisher

How can I mock URLSession.DataTaskPublisher? I have a class Proxy that require to inject a URLSessionProtocol protocol URLSessionProtocol { func loadData(from url: URL) -> URLSession.DataTaskPublisher } class Proxy { private let…
Tim
  • 151
  • 1
  • 10
1
2
3
52 53