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
5
votes
2 answers

How to use new Result type introduced in swift 5 URLSession?

Swift 5 introduces new Result type to handle the result of an asynchronous function. I want to know the way to use this new result type for URLSession. I have this following code. func getCategorByAPI() { //Base Url is from an static…
Asif Newaz
  • 557
  • 7
  • 17
5
votes
2 answers

Using Generics / Codable w/ API response 204 NO CONTENT

I am using generics and codable with URLSession. When I receive a response from an API, I check the status is in the 200 - 299 range and decode the data like so guard let data = data, let value = try? JSONDecoder().decode(T.self, from: data)…
Tim J
  • 1,211
  • 1
  • 14
  • 31
5
votes
2 answers

How do I perform an insecure URLSession query in iOS

I'm trying to perform a query to a website that I run. However, currently, this website's certificate is invalid (for a valid reason for now). I'm trying to query it with this code: private static func performQuery(_ urlString: String) { guard…
Wayneio
  • 3,466
  • 7
  • 42
  • 73
5
votes
1 answer

Subclassing factory methods of URLSession in Swift

I am trying to create a subclass of URLSession in Swift (reason does not matter, but has to do with testing). I need it to work with a delegate and a specific URLSessionConfiguration, which is a read-only property on URLSession. Usual way to…
Legoless
  • 10,942
  • 7
  • 48
  • 68
5
votes
0 answers

Modified URLSession.shared to implement certificate pinning, how did they do it?

On my project, we are using a closed source framework (Backbase, if you wanna know). This is an hybrid application framework that offers many security options "out of the box". One of them is certificate pinning, and I am quite intrigued by its…
hulius
  • 189
  • 1
  • 8
5
votes
1 answer

Swift: URLSession.shared.dataTask says status code 304 = 200?

I'm requesting a JSON-API like the following: var request = URLRequest(url: url) request.httpMethod = "GET" request.addValue(CredentialsProvider.shared.credentials, forHTTPHeaderField: "Authorization") let task =…
swalkner
  • 16,679
  • 31
  • 123
  • 210
4
votes
0 answers

URLSession: TLS ticket does not fit

When I get a very large response using URLSession, I am getting this warning: [quic] quic_crypto_session_state_serialize [C3.1.1:2] [-4b5609327fb472a4] TLS ticket does not fit (6817 > 6144) Maximum TLS record size is 16 KB, but here we can see only…
Dewerro
  • 371
  • 3
  • 12
4
votes
0 answers

URLSession request: request Code=-1009 "(null)" UserInfo={_NSURLErrorNWPathKey=unsatisfied (Denied over cellular interface)

Test is performed on iPhone 13 Pro 15.3.1, with Xcode 13.2.1. I have narrow down the issue as below two tests. The test code below just send a simple https request. I have allowed the network access for the test app on both Wifi and cell. And I'm a…
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32
4
votes
1 answer

Swift Combine in UIKit. URLSession dataTaskPublisher NSURLErrorDomain -1 for some users

After switching our API Client to Combine we start to receive reports from our users about error "The operation couldn’t be completed (NSURLErrorDomain -1.)" which is the error.localizedDescription forwarded to UI from our API client. Top level api…
George
  • 643
  • 9
  • 23
4
votes
1 answer

In Swift, how do you get the error type from UrlSession

I have the following code in my network logic: let task = urlSession.dataTask(with: request) { [weak self] (data, response, error) in if let error = error { if error.localizedDescription.contains("The request timed out") { //…
Fred
  • 381
  • 5
  • 17
4
votes
1 answer

Can't catch "Time out" error using URLSession

I am use URLSession to "POST" request, but sometimes when request on a road I get error code with difference error, more often: "Time out" This is example of error message that I saw in console "Error Domain=NSURLErrorDomain Code=-1009 "The…
Ice
  • 680
  • 1
  • 10
  • 23
4
votes
1 answer

How can I cancel the URLSession.shared.dataTask if I exit the page before it completes?

Following the recommendation from https://stackoverflow.com/a/27712427/3286489, I can perform asynchronous download as below, and it is called during viewDidLoad. func loadItems(tuple : (name : String, imageURL : URL)) { print("Download…
Elye
  • 53,639
  • 54
  • 212
  • 474
4
votes
1 answer

URLSessionWebSocketTask: Fatal error: Only one of message or error should be nil

I've been getting this error randomly since I recently used URLSessionWebSocketTask, which causes my program to crash every time: Fatal error: Only one of message or error should be nil But I can't find a specific way to reproduce it, because I…
4
votes
2 answers

Swift - Writing a generic HTTP request

So I am trying to retrieve some data using some normal HTTP requests with Swift's new Decodable protocol. The problem is that I am finding that I am writing very similar code again and again (create a URL session, decode the data, completion handler…
Vedant
  • 339
  • 4
  • 10
4
votes
0 answers

Class AVAssetDownloadTask is implemented in both CFNetwork and AVFoundation

When using Xcode 11.3 and running on watchOS 6.1.1, I see the following conflicts objc[45250]: Class AVAssetDownloadTask is implemented in both…
onmyway133
  • 45,645
  • 31
  • 257
  • 263
1 2
3
52 53