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
14
votes
8 answers

NSURLSession and amazon S3 uploads

I have an app which is currently uploading images to amazon S3. I have been trying to switch it from using NSURLConnection to NSURLSession so that the uploads can continue while the app is in the background! I seem to be hitting a bit of an issue.…
George Green
  • 4,807
  • 5
  • 31
  • 45
13
votes
4 answers

ios - download only few seconds from mp3 file

i need to download a mp3 file, but i only need the first 20 seconds of the song (or the entire song if the song is less than 20 sec). this is how i download the entire song: func downloadSong(audioUrl: URL) { let documentsDirectoryURL =…
Eyal
  • 10,777
  • 18
  • 78
  • 130
13
votes
0 answers

XCTest: NSURLSession: Stall on main thread

When running an XCTest that executes a network operation, i get the following error: -waitForExpectationsWithTimeout: timed out but was unable to run the timeout handler because the main thread was unresponsive (0.5 seconds is allowed after the wait…
FranticRock
  • 3,233
  • 1
  • 31
  • 56
13
votes
1 answer

Programmatically configure proxy settings in iOS

How to configure proxy settings in iOS programmatically in NSURLSession ? I followed this link. But it seems kCFStreamPropertyHTTPProxyHost, kCFStreamPropertyHTTPProxyPort are deprecated from iOS 9. SO what's the way to create proxy…
sasi kumar
  • 723
  • 5
  • 14
13
votes
1 answer

ios FTP upload using NSURLSession

I am trying to upload files via FTP to server. According to Apple Documentation NSURLSession class supports FTP operations. There is a famous Apple Developer blog which also supports that. But still its not clear whether NSURLSession API's supports…
Amit
  • 1,043
  • 1
  • 10
  • 32
13
votes
1 answer

NSURLSessionDownloadTask downloadTask: didFinishDownloadingToURL file does not exist?

I have implement NSURLSessionDownloadTask for downloading multiple video at a same time. In a normal scenario every thing is working fine. Also background fetch is also working. But when i close the application and restart the application and do the…
Ashwin P
  • 501
  • 1
  • 3
  • 19
13
votes
2 answers

iOS Swift : How to find if an NSURLSession has timed out

In the iOS app I am currently building, I am trying to show a message to the user when the session has timed out. I read the documentation for NSURLSessionDelegate but couldn't find out any method for letting me know if the session has timed out.…
SphericalCow
  • 345
  • 1
  • 2
  • 13
13
votes
2 answers

Does NSURLSession Take place in a separate thread?

I was designing an app that uses NSURLSession and thinking about putting it in a different thread with Grand Central Dispatch, but if NSURLSession automatically does that in the background, I wouldn't have to use GCD then, correct? So in other…
13
votes
1 answer

Upload NSURLSesssion becomes invalidated in sharing extension in ios8 with error NSURLErrorDomain Code=-995

I try to upload an image using NSURLSession in shared extension in iOS 8 but have this error instantly after calling [task resume] Error Domain=NSURLErrorDomain Code=-995 "The operation couldn’t be completed. (NSURLErrorDomain error -995.)" here…
sasha_nec
  • 532
  • 5
  • 12
13
votes
5 answers

iOS simulator access localhost server

I am trying to get rest data to iOS app, and I use: var rest_url = "http://192.168.0.1:8000/rest/users/" let url: NSURL = NSURL(string: rest_url) let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(url, completionHandler:…
Mirza Delic
  • 4,119
  • 12
  • 55
  • 86
13
votes
1 answer

NSURLSession post : difference between uploadTask and dataTask

These are my two examples : let config = NSURLSessionConfiguration.defaultSessionConfiguration() config.HTTPAdditionalHeaders = ["Accept": "application/json", "Content-Type": "application/json", …
Aymenworks
  • 423
  • 7
  • 21
13
votes
4 answers

Weird NSURLSessionDownloadTask behavior over cellular (not wifi)

I've enabled Background Modes with remote-notification tasks to download a small file (100kb) in background when the app receives a push notification. I've configured the download Session using NSURLSessionConfiguration *backgroundConfiguration =…
13
votes
2 answers

NSURLSession priority

I'm trying to port my old code for loading files from NSOperationQueue to NSURLSession. Almost everything is alright but I can't find how to set priority for loading different tasks. Does anybody know if NSURLSession supports prioritising at all?…
Rostyslav Druzhchenko
  • 3,673
  • 3
  • 33
  • 38
13
votes
1 answer

Converting NSURLSessionDataTask into a Download task with Background support

I need to download with background ability certain files (not all of them), after checking the headers (for length and types), but it has to be in the same operation, not creating a new task/request (because sometimes I get an error from the server…
eiprol
  • 389
  • 1
  • 3
  • 14
13
votes
4 answers

iOS: NSURLSession category methods not being found at runtime

I wanted to create a category on NSURLSession. The app compiles ok, but when I attempt to call the category methods I get -[__NSCFURLSession doSomething]: unrecognized selector sent to instance 0xbf6b0f0 :0: error: -[NSURLSession_UPnPTests…
drekka
  • 20,957
  • 14
  • 79
  • 135