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
2 answers

Swift NSURLSession Request to local server - no data sent or received

I'm attempting to write a simple API client in Swift 2.2 using NSURLSession for a server I'm running locally at port 3000. The server just serves up a static string of JSON. This works fine: $ curl http://localhost:3000 {"data":"value"} My Swift…
akowalz
  • 359
  • 3
  • 16
1
vote
1 answer

Trying to understand asynchronous calls

I have this method - (NSString*) createUserWithName:(NSString*)TheName { NSURL *URL =someUrlthatIncludesTheName NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; [request…
Sjakelien
  • 2,255
  • 3
  • 25
  • 43
1
vote
1 answer

defaultSessionConfiguration() works while app is in background

I'm coming across a behavior that is nowhere noted in apple's documentation. I'm making a call over the network using NSURLSession with a NSURLSessionConfiguration.defaultSessionConfiguration() object. According to apple's docs, this is supposed to…
Mihado
  • 1,487
  • 3
  • 17
  • 30
1
vote
0 answers

Data are not passed using NSUrlSession

I'm trying to call API which passes JSON data to server. Below is my code NSDictionary *dictParam = [[NSDictionary alloc] initWithObjectsAndKeys:@"ABC", @"username", @"abc@xyz.com", @"email", @"linkedin", @"social", nil]; if([NSJSONSerialization…
Ankita Shah
  • 2,178
  • 3
  • 25
  • 42
1
vote
1 answer

NSURLRequest with cachePolicy ReloadIgnoringCacheData return stale data

I have an usecase where i need to poll a rest api for authentication on Apple TV. I get stale response JSON response for 90-120 sec after which i am getting proper JSON response. Below is my code static func getFileNoCache(url:NSURL?,…
kiran
  • 41
  • 8
1
vote
1 answer

Call NSURLSession method from inside the subclass of NSOperation

I my application I have table view with custom cell, that has: @property (weak, nonatomic) IBOutlet UIImageView *image; @property (weak, nonatomic) IBOutlet UILabel *nameOfImage; @property (weak, nonatomic) IBOutlet UIButton *start; I need to…
Melany
  • 466
  • 7
  • 20
1
vote
0 answers

NSURLSession return no connection error when launching after sleep mode

In my app, I use NSURLSession to download data from a server. The download happens in two scenarios: 1. on app launch 2. if refresh button pushed I noticed one issue with the following operation: Open my app Put iPhone into sleep mode Awake…
1
vote
1 answer

NSURLSession and NSURLConnection usage as it relates to sharing a SSL session

I have some logic that communicates with a backend server which has two versions, a NSURLConnection and NSURLSession. The latter was made because NSURLConnection is deprecated, and also we want to leverage the background download ability. These…
Locksleyu
  • 5,192
  • 8
  • 52
  • 77
1
vote
1 answer

Cann't get http response when exception happens on server side in swift

I am using NSURLSession to request http post to server. It works fine if everything is ok. But I can't get the response when the server throw an exception. In browser, I should be able to get the 505 error code. But I can't get it in swift. Below is…
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
1
vote
1 answer

NSURLResponse kCFURLErrorNotConnectedToInternet on mobile connection

one of my iOS Apps uses NSURLSession to connect to a web service and query some data. I have just added and released this feature and in most cases it seems to work without any problems. However a hand full of customer report, that they are not able…
Andrei Herford
  • 17,570
  • 19
  • 91
  • 225
1
vote
1 answer

Showing progress bar on multiple image upload in UICollectionView in swift

I've a chat view which is designed using UICollectionView. I've successfully implemented image upload functionality where whenever a user clicks an image using image picker the image gets uploaded to the server. Now I'm trying to add progress bar to…
Francis F
  • 3,157
  • 3
  • 41
  • 79
1
vote
0 answers

How to clear TLS cache on iOS?

Our server validates client cert. When user A login and get cert A, then switch to account B, we issue another cert B, which requests should use now. According to https://developer.apple.com/library/mac/qa/qa1727/_index.html, we create another…
Nix Wang
  • 814
  • 10
  • 18
1
vote
1 answer

Common shared class for webservice call

Let me clear my question. I want a common shared class to call a webservice. And also I want to handle the data and error I get in the response using a Block. I got the answer for that. As Amin said I can make a shared insctance as below: +…
1
vote
1 answer

NSURLSession crash with JSON data parameter is nil while error is managed

This problem has already been mentioned elsewhere but the solution was dealing with error. And here I think I manage my error correctly (but it sound like I am mistaken...). I've got something strange I cannot figure out. My app crashes when I…
Trichophyton
  • 625
  • 5
  • 21
1
vote
1 answer

Downloading JSON data fails at NSHTTPURLResponse

I am trying to get a JSON file from a URL and return the contents using Swift. However, the code fails at the line let httpResponse = response as! NSHTTPURLResponse in the following code. I get an exception at this line and Xcode goes into debug…
Jake
  • 25,479
  • 31
  • 107
  • 168