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
11
votes
1 answer

Sending JSON POST request in Swift3

In my app i'm trying to make http post request with json data. The following json must be transferred to api { "Password":"123456", "Email":"test@gmail.com" } Here is my code for this task let dict = ["Email": "test@gmail.com",…
Vah.Sah
  • 522
  • 1
  • 6
  • 21
11
votes
3 answers

Cancel all web service calls from Alamofire

In my project there are 4 web services running in the background. I would like to stop all these services on logout from the current state without waiting for response. I have found this code for doing…
Janak Thakkar
  • 414
  • 5
  • 20
11
votes
2 answers

iOS and Go - Keep-Alive using NSURLSession

In my iOS app, I have a search feature that fetches results from a server. The search updates live as the user updates their query, so this results in several requests being made in succession. So my question is, how can I ensure that TCP keep-alive…
Hundley
  • 3,167
  • 3
  • 23
  • 45
11
votes
1 answer

Get the data from NSURLSession DownloadTaskWithRequest from completion handler

So I'm having hard time understanding something. This are the things I understand about NSURSession : Generally , I have 2 options for (as far as I know) DataTask(e.x dataTaskWithRequest) And DownloadTask(e.x DownloadTaskWithRequest) - Using their…
Jackky White
  • 357
  • 1
  • 4
  • 11
11
votes
2 answers

NSURLSession didFinishDownloadingToURL temporary downloaded file not found

I'm having a strange issue with NSURLSession on the delegate method didFinishDownloadingToURL. First thing I'm doing is check if the temporary downloaded file exist: - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask…
Guilherme Gusman
  • 556
  • 2
  • 7
  • 17
11
votes
3 answers

NSURLSession with Token Authentication

I have the following code in my iOS project and I want to convert to use NSURLSession instead of NSURLConnection. I am querying a REST API which uses a token-based HTTP Authentication scheme but I cannot find an example of how to do…
elenag
  • 153
  • 1
  • 2
  • 10
10
votes
2 answers

URLSession: use http/1.1 instead of http/2

I saw that URLSession supports HTTP 1.1 and HTTP 2 (URLSession supports the HTTP/1.1 and HTTP/2 protocols). How can I enforce URLSession to use HTTP1.1?
swalkner
  • 16,679
  • 31
  • 123
  • 210
10
votes
2 answers

Background upload with share extension

I created an macOS ShareExtension which I want to use to upload pictures. I'm still testing this so any requests will be sent to https://beeceptor.com. The share extension works fine and it shows up in Preview, once I run it: I add some text and…
Besi
  • 22,579
  • 24
  • 131
  • 223
10
votes
4 answers

URLSession.datatask with request block not called in background

URLSession data task block is not calling when the app is in background and it stuck at dataTask with request. When I open the app the block gets called. By the way I'm using https request. This is my code: let request = NSMutableURLRequest(url:…
Test Test
  • 1,761
  • 3
  • 10
  • 12
10
votes
2 answers

how to support incognito/private mode in wkwebview/uiwebview

I am working on a incongnito browser.I am using wkwebview when I clear all the cookies I can see that popular search engine like google remembers the searches that has been made. I tried cleaning all the cookies in NSHTTPCookieStorage and…
Shiva
  • 545
  • 1
  • 10
  • 41
10
votes
1 answer

'didReceiveChallenge' not called for HttpBasic authentication after ServerTrust

I am converting an iOS app from NSURLConnection to NSURLSession. The server is interacts with uses both https (cert signed by a recognised CA) and Basic Authentication. Rather than using completion blocks for the data return, I'm using custom…
Snips
  • 6,575
  • 7
  • 40
  • 64
10
votes
1 answer

NSURLSession HTTP/2 memory leak

This My Test cases, point out that when using NSURLSession with a HTTP/2 connection there is memory problem. test1: iOS 9. HTTP/2 server I use NSURLSession to upload 10M file to a HTTP/2 server, if the file uploaded completed everything is ok, But…
rotoava
  • 625
  • 8
  • 18
10
votes
2 answers

Request Timed out with Code=-1001_kCFStreamErrorCodeKey=-2102

Sometimes network request from my app gets stuck, And I get the following error after a while, failed with error: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."UserInfo={NSUnderlyingError=0x1390937f0 {Error…
Shimin
  • 101
  • 1
  • 3
10
votes
2 answers

URLSessionDidFinishEventsForBackgroundURLSession Not Calling- Objective-C

NSURLSession Delegate method URLSessionDidFinishEventsForBackgroundURLSession is not Calling ? I already enabled the Background Modes in project capabilities settings. Here is the code AppDelegate.h Method @interface AppDelegate : UIResponder…
iReddy
  • 467
  • 1
  • 5
  • 12
10
votes
3 answers

Find the Download Progress of a file in swift

I have searched but haven't found a relevant answer only in Objective C. Is there a way to find the progress of the download of a file in Swift, so that to show it to user? I am new to iOS programming and I have tried with NSURLSession but without…
loopidio
  • 423
  • 1
  • 5
  • 11