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
68
votes
4 answers

Invalid conversion from throwing function of type (_,_,_) throws -> Void to non-throwing function type (NSData?, NSURLResponse?, NSError?) -> Void

I have written this code: func getjson() { let urlPath = "https://api.whitehouse.gov/v1/petitions.json?limit=100" let url = NSURL(string: urlPath) let session = NSURLSession.sharedSession() let task =…
Martin Mikusovic
  • 1,002
  • 2
  • 9
  • 14
39
votes
3 answers

HTTP load failed (error code: -999 [1:89]) for Task in iOS 11

I'm using URLSession to perform a datatask, when I run it on iOS 11, I get a console error: HTTP load failed (error code: -999 [1:89]) for Task <68809C58-C6A7-4F10-86A4-81396D8B18CF>.<2> Any thought on what's causing it, or how to fix it?
Ennabah
  • 2,303
  • 2
  • 20
  • 39
14
votes
4 answers

Swift return data from URLSession

I cannot return data from my HTTPrequest and I can't get completion handlers to work either. So please assist me in my quest to solve this issue: public static func createRequest(qMes: message, location: String, method: String) -> String{ let…
Bram
  • 2,718
  • 1
  • 22
  • 43
13
votes
4 answers

How to cache images using URLSession in Swift

I would like to enhance the code below to cache images and only download them if they haven't been cached previously. I can't seem to find any good examples of how to use URLSession object to do this. extension UIImageView { func…
Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55
12
votes
2 answers

How to download images async for WidgetKit

I am developing Widgets for iOS and I really don't know how to download images for the widgets. The widget currently downloads an array of Objects, and every object has a URL of an image. The idea is that every object makes a SimpleEntry for the…
Joan Cardona
  • 3,463
  • 2
  • 25
  • 43
10
votes
1 answer

URLSession caching not working as documented - what am I doing wrong?

According to the documentation if you use the default useProtocolCachePolicy the logic is as follows: If a cached response does not exist for the request, the URL loading system fetches the data from the originating source. Otherwise, if the…
mluisbrown
  • 14,448
  • 7
  • 58
  • 86
10
votes
1 answer

Refresh access token with URLSession after getting a 401 response code & retry request

I'm working on building a networking client for my iOS application which uses OAuth 2.0 Authorization techniques (Access & Refresh Token). There is a feature for my networking client that I have been struggling to implement: When a 401 error occurs…
CoderMan
  • 178
  • 3
  • 12
10
votes
0 answers

TIC TCP Conn Failed

Something have changes in the server I was using to fetch an .mjpeg video feed. Now I'm getting this error: TIC TCP Conn Failed [5:0x1d4361380]: 3:-9802 Err(-9802) TIC TCP Conn Failed [6:0x1c0177a00]: 3:-9800 Err(-9800) TIC TCP Conn Failed…
Stornu2
  • 2,284
  • 3
  • 27
  • 47
10
votes
3 answers

Swift URL Session and URL Request not working

I am getting very similar problems to this post, but I don't fully understand the answer. I've created a completion handler, but it doesn't seem to be working as expected. func updateTeam(teamID: Int) { startConnection {NSArray, Int in …
D. Cohen
  • 567
  • 1
  • 7
  • 22
8
votes
1 answer

Using URLCache subclasses with URLSession

I have an app which uses URLSession-based networking and URLCache for storing network requests on disk. I noticed that when the storage size of URLCache reaches the diskCapacity, the eviction strategy seems to be to remove all entries, which is a…
Nikita Zhuk
  • 151
  • 7
8
votes
3 answers

With Combine, how to deallocate the Subscription after a network request

If you use Combine for network requests with URLSession, then you need to save the Subscription (aka, the AnyCancellable) - otherwise it gets immediately deallocated, which cancels the network request. Later, when the network response has been…
Rob N
  • 15,024
  • 17
  • 92
  • 165
8
votes
0 answers

URLSession in swift 4 for uploading jpeg file from iOS gallery generating Error

I am trying to make uploading of my image from iPhone gallery to my web-server running on PHP. For uploading request I am using URLSession and for selecting images from iPhone Gallery UIImagePicker. I am getting this error from my iPhone 7 and…
8
votes
0 answers

Prioritisation of networking calls on iOS

I want make use of good prioritisation of networking calls on iOS. So far, I am using URLSessionTask.priority. I create networking calls like this: URLSession.shared.dataTask(with: request) { data, response, error in if let data = data, let…
Sebastian Hojas
  • 4,158
  • 2
  • 27
  • 38
7
votes
2 answers

Why do I see an error about 'data(for:delegate:) is only available on iOS 15.0+' even though modern concurrency is backward compatible

Modern concurrency with the new Async / Await was introduced for iOS 15 and above with Swift 5.5 but very soon, with the release of Xcode 13.2 (and subsequently 13.2.1) it enabled us to use Async and Await to develop for iOS 13+, macOS 10.15+ etc.…
Sabesh Bharathi
  • 171
  • 1
  • 9
7
votes
3 answers

Cancelling an async/await Network Request

I have a networking layer that currently uses completion handlers to deliver a result on the operation is complete. As I support a number of iOS versions, I instead extend the network layer within the app to provide support for Combine. I'd like to…
Harry Blue
  • 4,202
  • 10
  • 39
  • 78
1
2 3
52 53