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

NSURLSessionTask never calls back after timeout when using background configuration

I am using NSURLSessionDownloadTask with background sessions to achieve all my REST requests. This way I can use the same code without have to think about my application being in background or in foreground. My back-end has been dead for a while,…
20
votes
3 answers

NSURLSession background download - resume over network failure

After reading the Apple documentation about the background download with the new iOS7 api (NSURLSession), I'm a bit disappointed. I was sure that Apple was managing the pause/resume over the network availability in the background (or provide an…
Nicolas Lauquin
  • 1,517
  • 3
  • 14
  • 23
19
votes
1 answer

NSURLConnection deprecated in iOS9

I want to download a file with a NSURLRequest and save it but in the line with the NSData * data = ... happens an error. NSURL *Urlstring = [NSURL URLWithString:@"http://yourdomain.com/yourfile.pdf"]; NSURLRequest *request = [NSURLRequest…
Maximilian
  • 754
  • 1
  • 5
  • 26
19
votes
1 answer

How to cancel on-going HTTP request in Swift?

My code does GET request like this: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in // ... let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in …
Joon. P
  • 2,238
  • 7
  • 26
  • 53
19
votes
6 answers

NSURLSession completion block not called

var session = NSURLSession.sharedSession() session.dataTaskWithRequest(urlRequest, completionHandler: {(data: NSData!, response: NSURLResponse!, …
esh
  • 2,842
  • 5
  • 23
  • 39
19
votes
3 answers

How do you know when the NSURLSession object has been invalidated by iOS?

I am seeing an error in my tests where occasionally I get the following iOS error: A background URLSession with identifier {GUID} already exists! Even though I call invalidateAndCancel on the NSURLSession in the cleanup call after every test. I am…
Spilly
  • 1,069
  • 2
  • 9
  • 16
18
votes
3 answers

How to resume NSURLSession download process after app force-quit and app relaunch?

I have implemented NSURLSession for downloading fairly large files from our servers. Now as long as I'm working in foreground or background and go back to the app the transactions are working and getting finished. But if I force-quit the app using…
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
18
votes
1 answer

How to use special character in NSURL?

My application is using an NSURL like this: var url = NSURL(string: "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country=") When I tried to make a task for getting data from this NSURL like this: let task =…
Hieu Duc Pham
  • 1,074
  • 1
  • 10
  • 24
18
votes
2 answers

Set cookies with NSURLSession

Hi I am developing one Iphone application In which I want to set cookies after server response and use that for another request. My network request looks like. NSURLSession *session = [NSURLSession sharedSession]; [[session dataTaskWithURL:url…
nilkash
  • 7,408
  • 32
  • 99
  • 176
18
votes
4 answers

Average progress of all the NSURLSessionTasks in a NSURLSession

An NSURLSession will allow you to add to it a large number of NSURLSessionTask to download in the background. If you want to check the progress of a single NSURLSessionTask, it’s as easy as double taskProgress = (double)task.countOfBytesReceived /…
Eric
  • 16,003
  • 15
  • 87
  • 139
18
votes
2 answers

iOS: Does force quitting the app disables background upload using NSURLSession?

The question is around NSURLSession and NSURLSessionUploadTask. I'm uploading large files to server and noticed that when I force quit the app the whole background upload just stops. However, when upload starts while app is running through the…
ymotov
  • 1,449
  • 3
  • 17
  • 28
17
votes
2 answers

unable to upload file using NSURLSession multi-part form data in iOS

I am trying to upload a video / image file using- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL; method using multi-part form data. But somehow i am not able to upload the file and i am getting…
17
votes
1 answer

How does one deal with a cancelled NSURLSessionTask in the completion handler block?

If I create a NSURLSessionDownloadTask, and later cancel it before it finishes, the completion block still fires seemingly. let downloadTask = session.downloadTaskWithURL(URL, completionHandler: { location, response, error in ... } How do I…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
16
votes
4 answers

ETag and If-None-Match HTTP Headers are not working

I have a file in my webserver and I am downloading that to my app everytime I access it because its possible that file content might be changed But If it is changed I would like to download that time only so bandwidth can be saved and fortunately…
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
16
votes
2 answers

Making HTTP Request with header in Swift

I am trying to make an HTTP request to the Imgur API. I am trying to retrieve all images associated with the tag "cats." The url, according to the Imgur API is: https://api.imgur.com/3/gallery/t/cats the Imgur API states the following about the…
jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72