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
1
vote
1 answer

NSURLSession with DataTask Delegate return expectedContentLength negative(-1) , Cant calculate file size

I'm trying to calculate the data that is being received in dataTaskWithURL for a progress bar using this follow up delegate method: NSURLSessionTaskDelegate, NSURLSessionDataDelegate, this is the code I use in the view didload, I'm not using…
Jackky White
  • 357
  • 1
  • 4
  • 11
1
vote
1 answer

Is there any way to enable NSURLSession to run more than 4 NSURLSessionUploadTasks concurrently?

When I create and start 20 NSURLSessionUploadTasks only 3-4 will run at anyone time, even when HTTPMaximumConnectionsPerHost is set to 20! I understand that I can limit the number of max uploads very easily with HTTPMaximumConnectionsPerHost.…
1
vote
1 answer

Best practise to Use NSURLSession to receive data from the server on the background(not on the main theard)

How to receive data from the server(the server echo's it as JSON) using NSURLSession in the most efficient way? Example: I'm trying to use Instagram's news feed as a save template. They show 10 images and then it will load 10 more(from the server)…
1
vote
0 answers

Swift iOS 8.2 EXEC_BAD_INSTRUCTION in NSURLSession

I'm trying to get a json in my server but the request stops with error EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) when running IPhone 6, 6 plus, 5 and 5s iOS Simulator. Nothing happens when i use Iphone 4s simulator and the app works…
1
vote
0 answers

NSURLSession: "Background URL session needs events handled"

I'm performing an upload task in the background with NSURLSession. I keep seeing this message in the output: "Background URL session needs events handled" Where should I be handling the event for this? In the app delegate, in the class with my…
Andrew Paul Simmons
  • 4,334
  • 3
  • 31
  • 39
1
vote
1 answer

Implement MKNetworkKit's caching style with AFNetworking 2.0

A newtwork library - MKNetworkKit, that was popular long time ago had an interesting and useful cache style, as docs say: MKNetworkKit can automatically cache all your “GET” requests. When you make the same request again, MKNetworkKit calls your…
alex
  • 2,121
  • 1
  • 18
  • 24
1
vote
0 answers

Swift NSMutableURLRequest parse multipart response data

I've been searching a while for a solution to this problem but everyone seems to be on the opposite side that I'm on. I'm writing some swift code to receive a multipart/form-data response from a Django REST API (also written by me). The response…
1
vote
0 answers

How should I save data while is uploading some data with NSURLSession and the app is suspended?

I am developing an app where I have to upload an entity with its relation. (I think this is similar to dropbox files uploading) This entity is called PlaceEntity and has a relation with PictureEntity (to many). I want to do it using NSURLSession to…
croigsalvador
  • 1,993
  • 2
  • 24
  • 47
1
vote
2 answers

Return value from asynchronous thread

In a playground file, I have a function which is supposed to return a array where Lesson is a custom data type. In the playground at the right hand of my screen it does display a array with the values that where supposed to be in the array though if…
milo526
  • 5,012
  • 5
  • 41
  • 60
1
vote
1 answer

Is it possible to use AVAssetExportSession's `exportAsynchronouslyWithCompletionHandler:` method when the app is in the background?

I'm working on an iOS app, uploading videos from the Camera Roll, using NSURLSession with a background configuration. The user can queue up multiple videos for upload (the queue is executed serially). A single upload consists of: Getting an…
Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74
1
vote
0 answers

Crash after NSURLSession task resume

I am using NSURLSession to do my network calls. Eventhough I am in the foreground, I prefer to use backgroundSession configuration because even if my app crashes the network call won't fail. However, after crash when I create the session with the…
mohkhan
  • 11,925
  • 2
  • 24
  • 27
1
vote
0 answers

downloadTaskWithRequest in NSURLSession swift

I would like to ask a question about download file issue. I want to show the download progress to user. Therefore, I implement the class and code as below in playground, please see [CODE 1]. When I execute the code as below [Scenario 1], we can get…
Tommy Liu
  • 239
  • 4
  • 13
1
vote
0 answers

NSURLSession delegate for basic auth called only after iOS Default Dialog box dismissed

I'm fetching an http document through a Squid proxy with Basic Auth, using an instance of objective-c's NSURLSession class on iOS8. I have implemented delegate methods for both URLSession:didReceiveChallenge:completionHandler:…
Jeff
  • 6,646
  • 5
  • 27
  • 33
1
vote
0 answers

NSURLSession keeps giving errors like 1001, 1003, 1005 etc

I have recently moved from NSURLConnection to NSURLSession i.e., I have replaced all implementations of NSURLConnection with NSURLSession. I have done this so that my code is up to date. Everything works fine except that I now keep getting a lot of…
1
vote
1 answer

NSURLSession self as delegate in init or inner class?

I can't understand why I'm getting a 'self' used before super.init call when creating the NSURLSession in the init. This is the code I am using: class MyClass: NSObject, NSURLSessionDataDelegate{ private let urlSession: NSURLSession …
Andres
  • 11,439
  • 12
  • 48
  • 87