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.

2431 questions
544
votes
21 answers

The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

I am facing the Problem when I have updated my Xcode to 7.0 or iOS 9.0. Somehow it started giving me the Titled error "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection" Webservice…
Manab Kumar Mal
  • 19,410
  • 5
  • 29
  • 43
408
votes
19 answers

How do I make an HTTP request in Swift?

I read The Programming Language Swift by Apple in iBooks, but cannot figure out how to make an HTTP request (something like cURL) in Swift. Do I need to import Obj-C classes or do I just need to import default libraries? Or is it not possible to…
Dicky Tsang
  • 5,425
  • 3
  • 17
  • 18
222
votes
7 answers

Send POST request using NSURLSession

Update: Solution found. You can read it at the end of the post. I'm trying to perform a POST request to a remote REST API using NSURLSession. The idea is to make a request with two parameters: deviceId and textContent. The problem is that those…
Sendoa
  • 4,675
  • 5
  • 25
  • 21
137
votes
13 answers

NSURLSession/NSURLConnection HTTP load failed on iOS 9

Tried to run my existing app on iOS9 but getting failure while using AFURLSessionManager. __block NSURLSessionDataTask *task = [self.sessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject,…
Tariq
  • 9,514
  • 11
  • 55
  • 97
109
votes
6 answers

Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit

I'm writing StoreKit-related code, and I'm getting some rather troubling error codes when I try to add a purchase to the queue. So far, I've experienced error codes -1003 and -1004 and I can't find anything about those codes on the internet. Running…
Hyperbole
  • 3,849
  • 3
  • 33
  • 54
95
votes
10 answers

Generate your own Error code in swift 3

What I am trying to achieve is perform a URLSession request in swift 3. I am performing this action in a separate function (so as not to write the code separately for GET and POST) and returning the URLSessionDataTask and handling the success and…
Rikh
  • 3,710
  • 2
  • 12
  • 33
74
votes
7 answers

NSURLSession: How to increase time out for URL requests?

I am using iOS 7's new NSURLSessionDataTask to retrieve data as follows: NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest: request completionHandler: ^(NSData *data, NSURLResponse…
AlexR
  • 4,907
  • 8
  • 63
  • 124
69
votes
1 answer

NSURLSession concurrent requests with Alamofire

I'm experiencing some strange behaviour with my test app. I've about 50 simultaneous GET requests that I send to the same server. The server is an embedded server on a small piece of hardware with very limited resources. In order to optimize the…
Hannes
  • 3,553
  • 2
  • 34
  • 43
55
votes
3 answers

NSURLSession with NSBlockOperation and queues

I have an app that currently uses NSURLConnection for the vast majority of its networking. I would like to move to NSURLSession because Apple tells me that is the way to go. My app just uses the synchronous version of NSURLConnection by way of the +…
Erik Allen
  • 1,743
  • 2
  • 19
  • 35
54
votes
7 answers

How do I use NSOperationQueue with NSURLSession?

I'm trying to build a bulk image downloader, where images can be added to a queue on the fly to be downloaded, and I can find out the progress and when they're done downloading. Through my reading it seems like NSOperationQueue for the queue…
Doug Smith
  • 27,683
  • 54
  • 189
  • 363
51
votes
4 answers

NSURLSession dataTaskForRequest:completion: unrecognized selector sent to instance

When trying to create my own session object NSURLSession() and request an url I get an unrecognized selector exception but when I use the shared session NSURLSession.sharedSession() everything works fine. How come? var url = NSURL(string:…
Alexey
  • 6,552
  • 8
  • 54
  • 92
48
votes
1 answer

How to get server response data in NSURLSession without completion block

I am using NSURLSession for background image uploading. And according to uploaded image my server gives me response and I do change in my app accordingly. But I can't get my server response when my app uploading image in background because there is…
user3648985
47
votes
4 answers

How to send POST and GET request?

I want to send my JSON to a URL (POST and GET). NSMutableDictionary *JSONDict = [[NSMutableDictionary alloc] init]; [JSONDict setValue:"myValue" forKey:"myKey"]; NSData *JSONData = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions…
Aleksander Azizi
  • 9,595
  • 8
  • 57
  • 86
38
votes
2 answers

Unexpected Non-Void Return Value In Void Function (Swift 2.0)

I have been skimming the StackOverflow questions trying to figure out where I'm going wrong with my code, but I just can't seem to! I am trying to convert my Swift 1.2 project to Swift 2.0, and am having an issue with my class that downloads JSON…
ZbadhabitZ
  • 2,461
  • 1
  • 18
  • 39
35
votes
3 answers

Resume NSUrlSession on iOS10

iOS 10 is going to be released soon so it worth to test applications for compatibility with it. During such test we've discovered that our app can't resume background downloads on iOS10. Code that worked well on previous versions does not work on…
Alexey Guseynov
  • 4,743
  • 1
  • 15
  • 28
1
2 3
99 100