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
26
votes
3 answers

How to cache using NSURLSession and NSURLCache. Not working

I have a test app setup and it successfully downloads content from the network even if the user switches apps while a download is in progress. Great, now I have background downloads in place. Now I want to add caching. There is no point to me…
John Erck
  • 9,478
  • 8
  • 61
  • 71
25
votes
4 answers

iOS 9.3 : An SSL error has occurred and a secure connection to the server cannot be made

I am getting following error with self signed certificate Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made. while testing web-services for one of my demo app with iOS…
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
25
votes
4 answers

NSURLSessionConfiguration timeoutIntervalForRequest vs NSURLSession timeoutInterval

With the new NSURLSession, there is now a timeoutIntervalForRequest on the NSURLSessionConfiguration object used to create the session. But there is still a timeoutInterval on the NSURLRequest object that can be used to create the NSURLSessionTask…
Joe Licari
  • 768
  • 1
  • 8
  • 6
24
votes
3 answers

Domain=NSURLErrorDomain Code 1202

Hi I receive the following error: Error Domain=NSURLErrorDomain Code=-1202 \"The certificate for this server is invalid. You might be connecting to a server that is pretending to be “server-prod.name-cloud.com” which could put your confidential…
Bar
  • 603
  • 1
  • 7
  • 19
24
votes
4 answers

Prevent NSURLSession from caching responses

Why does it cache responses. It returns previously fetched responses. It even works if turning off the network connection. Resetting the iOS simulator did not seem to work either. Making a request, then again with internet offline does work…
Sunkas
  • 9,542
  • 6
  • 62
  • 102
24
votes
1 answer

Transition from RestKit to pure AFNetworking 2.0

I'd been using RestKit for the last two years, but recently I've started thinking about transition from these monolith framework as it seems to be really overkill. Here's my pros for moving forward: There is big need in using NSURLSession for…
24
votes
4 answers

NSURLSession Memory Leaks occur when using web services in IOS

I am building an app that uses a web service and to get information from that web service I use NSURLSession and NSURLSessionDataTask. I am now in the memory testing phase and I have found that NSURLSession is causing memory leaks. This is not all…
Jonathan
  • 2,623
  • 3
  • 23
  • 38
24
votes
4 answers

Best practices for making a queue of NSURLSessionTasks

What are the best practices for making a serial queue of NSURLSessionTasks ? In my case, I need to: Fetch a URL inside a JSON file (NSURLSessionDataTask) Download the file at that URL (NSURLSessionDownloadTask) Here’s what I have so far: session…
Eric
  • 16,003
  • 15
  • 87
  • 139
22
votes
1 answer

Does NSURLSession for HTTP data task (NSURLSessionDataTask) runs in background thread or we will have to provide the queue?

I started to use NSURLSession by avoiding NSURLConnection now a days as it's a new and elegant API provided by Apple. Previously, I used to put call NSURLRequest in GCD block to execute it in background. Here is how I used to do in…
22
votes
1 answer

NSURLSession delegate vs. completionHandler

I've always used completion handlers. With NSURLConnection and now with NSURLSession. It's led to my code being really untidy, especially I have request within request within request. I wanted to try using delegates in NSURLSession to implement…
AndrewSB
  • 951
  • 3
  • 11
  • 25
21
votes
5 answers

iOS 9 ATS SSL error with supporting server

I installed Xcode 7 and tried running my app under iOS 9. I'm getting the infamous error: Connection failed! Error - -1200 An SSL error has occurred and a secure connection to the server cannot be made. The thing is my server DOES support TLSv1.2…
YogevSitton
  • 10,068
  • 11
  • 62
  • 95
21
votes
1 answer

iOS - Best practice to save Images locally - NSCache vs Save in Document Directory

I'm developing an app that similar to Instagram feed (tableviews with cells that contain images and some labels). For all the data I'm getting from the database, I'm using Data Task (because it doesn't take much to receive them), but for the images…
Jackky White
  • 357
  • 1
  • 4
  • 11
21
votes
1 answer

What happens if disk space runs out while using NSURLSessionDownloadTask in background?

In a iOS 8.1 app I am using NSURLSessionDownloadTask to download an archive in the background which can sometimes get quite large. Everything works fine, but what will happen if the phone runs out of disk space? Will the download fail and indicate…
Erik
  • 11,944
  • 18
  • 87
  • 126
20
votes
1 answer

Why is NSURLSession slower than cURL when downloading many files?

I've been using cURL to download about 1700+ files -- which total to about ~290MB -- in my iOS app. It takes about 5-7 minutes on my Internet connection to download all of them using cURL. But since not everyone has fast internet connection…
Hasyimi Bahrudin
  • 849
  • 1
  • 8
  • 12
20
votes
6 answers

How to find and cancel a task in NSURLSession?

I'm using an NSURLSession object to load images in my application. That could be loading several images simultaneously. In some moments I need to cancel the loading of one specific image and continue loading others. Could you suggest the correct…
Rostyslav Druzhchenko
  • 3,673
  • 3
  • 33
  • 38