Questions tagged [nsurlsession]

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

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

A NSURLSession 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. NSURLSession provides status and progress properties, of which support canceling, resuming, or suspending tasks, and resuming suspended, canceled, or failed downloads.


Types of Sessions

NSURLSession 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.

2479 questions
16
votes
4 answers

Memory leak when using NSURLSession.downloadTaskWithURL

So I hit another roadblock in my endeavors with Swift. I am trying to load multiple images into an image gallery - all works fine except of one thing. The memory use of the application keeps growing and growing despite the fact that I clear the…
Fygo
  • 4,555
  • 6
  • 33
  • 47
16
votes
2 answers

setting limit on concurrent tasks in AFNetworking 2 running AFHTTPSessionManager

so I know that in the old AFNetworking this was possible using the AFHTTPClient, and I know that if I use AFHTTPRequestOperationManager I can set the queue's limit, but I can't make AFHTTPSessionManager to run only x requests at a time without…
16
votes
3 answers

Is there any way to attach a NSDictionary of parameters to an NSURLRequest instead of manually making a string?

AFNetworking allows you to add an NSDictionary of parameters to a request, and it will append them to the request. So if I wanted to do a GET request with ?q=8&home=8888 I'd just making an NSDictionary like @{@"q": @"8", @"home": @"8888"} very…
user2005643
16
votes
3 answers

iOS/Cocoa - NSURLSession - Handling Basic HTTPS Authorization

[edited to provide more information] (I'm not using AFNetworking for this project. I may do so in future, but wish to resolve this problem/misunderstanding first.) SERVER SETUP I cannot provide the real service here, but it is a simple, reliable…
Womble
  • 4,607
  • 2
  • 31
  • 45
15
votes
4 answers

URLSession doesn't pass 'Authorization' key in header swift 4

I am trying to pass authorization key in header of a URLRequest. But at the server end the key is not received. The same API when called from postman working fine. Any other key in the header is working fine, even authorizations key is visible at…
Santosh
  • 1,254
  • 2
  • 16
  • 31
15
votes
2 answers

Pattern for retrying URLSession dataTask?

I'm fairly new to iOS/Swift development and I'm working on an app that makes several requests to a REST API. Here's a sample of one of those calls which retrieves "messages": func getMessages() { let endpoint = "/api/outgoingMessages" let…
bmt22033
  • 6,880
  • 14
  • 69
  • 98
15
votes
4 answers

Custom NSURLProtocol with NSURLSession

I'm trying to implement this tutorial which implements a custom NSURLProtocol with NSURLConnection. https://www.raywenderlich.com/76735/using-nsurlprotocol-swift It works as expected, but now that NSURLConnection is deprecated in iOS9, I'm trying to…
kupilot
  • 494
  • 1
  • 3
  • 15
15
votes
2 answers

NSURLSession Threads: Tracking multiple background downloads

So I'm creating my download on the main thread NSURLRequest *request = [NSURLRequest requestWithURL:download.URL]; NSURLSessionDownloadTask *downloadTask = [self.downloadSession downloadTaskWithRequest:request]; [downloadTask resume]; and adding…
Andrew
  • 250
  • 1
  • 3
  • 10
14
votes
4 answers

How to know if NSURLSessionDataTask response came from cache?

I would like to determine if the response from NSURLSessionDataTask came from cache, or was served from server I'am creating my NSURLSessionDataTask from request.cachePolicy = NSURLRequestUseProtocolCachePolicy;
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
14
votes
2 answers

Download in documents directory or move file async - iOS

I am building an iOS app in which the user can download different files. I am using an URLSessionDownloadTask and an URLSession to download a file asynchronously.When the download is finished, the destination folder is by default, the tmp/…
MadreDeDios
  • 271
  • 2
  • 9
14
votes
2 answers

NSURLSessionUploadTask how to read server response

I am using NSURLSessionUploadTask to upload a file. Here are some parts of my code not complete let session:NSURLSession = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue .mainQueue()) let…
14
votes
1 answer

Background upload multiple images using single NSURLSession uploadTaskWithRequest

I want to upload multiple images in background using a single uploadTaskWithRequest method. While trying the following code returns Upload tasks from NSData are not supported in background sessions...please how to achieve this func createRequest…
siva ganesh
  • 173
  • 1
  • 7
14
votes
1 answer

AFNetworking 3.0 AFHTTPSessionManager using NSOperation

I'm stuck now some time and I need help. So in AFNetworking 2.0 we have AFHTTPRequestOperation so I could easily use NSOperationQueue and have some dependencies. So what we have now is only AFHTTPSessionManagerand NSURLSession that does not subclass…
Flipper
  • 1,107
  • 1
  • 11
  • 32
14
votes
2 answers

Random and occasional network error (NSURLErrorDomain Code=-1001 and NSURLErrorDomain Code=-1005)

The last couple of days I've tried to debug a network error from d00m. I'm starting to run out of ideas/leads and my hope is that other SO users have valuable experience that might be useful. I hope to be able to provide all relevant information,…
Steffen D. Sommer
  • 2,896
  • 2
  • 24
  • 47
14
votes
2 answers

iOS: Data Sessions vs Ephemeral Sessions

My app's webView loads a page and I inject some javascript which automates a click for me and adds an item to my wish list. For something like this would you recommend using a data session or an ephemeral session to load the page? Speed is important…
KingPolygon
  • 4,753
  • 7
  • 43
  • 72