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
13
votes
5 answers

Can't move file after background download: "No such file"

I have an app using a backgroundSessionConfiguration instance of NSURLSession to handle some NSURLSessionDownloadTask tasks. The tasks are created correctly, and finish downloading, but in URLSession:downloadTask:didFinishDownloadingToURL: when I go…
Farski
  • 1,670
  • 3
  • 15
  • 30
13
votes
1 answer

iOS 7 - NSURLSession Resumable Uploads

I'm currently in the process of designing a system for uploading multiple images to a server from an application. The files will be stored on the local device and the current idea is to split them into chunks and then upload them chunk-by-chunk to…
Ryan Burke
  • 942
  • 7
  • 13
12
votes
3 answers

What's the correct usage of URLSession, create new one or reuse same one

I am using URLSession in my iOS project. (Swift 4). The following code is only for illustration purpose. class MyTaskManager { ... func postMyData(...) { let defaultSession = URLSession(configuration: .default) dataTask =…
Leem
  • 17,220
  • 36
  • 109
  • 159
12
votes
1 answer

Swift:URLSessionDownload file says it exists but it doesn't?

Ok, I have succeeded in downloading various m4a files as well as removing them via URLSession. My problem is in the final "completion" function of the URLSessionDownloadDelegate requirement, I sometimes get the following printed to console even…
blue
  • 7,175
  • 16
  • 81
  • 179
12
votes
1 answer

Keep Alive not working properly on iOS

I am currently developing an application where we need some request to hit our server ASAP. To speed up the request process we have to eliminate handshake (as it takes extra) and have a permanent connection. The application is using the Alamofire…
zirinisp
  • 9,971
  • 5
  • 32
  • 38
12
votes
3 answers

URLSessionUploadTask getting automatically cancelled instantly

I'm having this weird issue in which a newly created URLSessionUploadTask gets cancelled instantly. I'm not sure if it's a bug with the current beta of Xcode 8. I suspect it might be a bug because the code I'm about to post ran fine exactly once. No…
Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
12
votes
3 answers

Erase all cookies in NSURLSession iOS

How can I erase all cookies from the NSHTTPCookieStorage.sharedHTTPCookieStorage? The only methods I am aware of delete a single specified cookie, however, the cookies are handled behind the scenes by NSURLSession. (Programming in Swift)
Andrew Johnson
  • 579
  • 4
  • 23
12
votes
2 answers

iOS9 - HTTP Connection Error

I got 3 error when i update my Xcode 1- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. 2- CFNetwork SSLHandshake failed…
Ibrahim BOLAT
  • 223
  • 1
  • 6
  • 18
12
votes
3 answers

Send local notification when download completes through NSURLSession / NSURLSessionDownloadTask

I am using NSURLSessionDownloadTask objects on an NSURLSession to allow users to download documents while the app is in the background / device locked. I also want to inform the user that individual downloads have finished through a local…
12
votes
6 answers

HTTP Request in swift not working

I'm trying to learn about working with APIs in Swift. As a good first test, I figured I'd hit the itunes API and just return some search results. I'm in playground using the following code. I don't get any errors, but my println isn't outputting…
user2874270
  • 1,312
  • 2
  • 18
  • 31
12
votes
6 answers

How to programmatically add a proxy to an NSURLSession

Looking over the documentation of NSURLSession and NSURLSessionConfiguration, I was under the impression I should configure it with a dictionary like the following: // Create a dictionary to describe the proxy NSDictionary *proxyDict = @{ …
Jeff
  • 6,646
  • 5
  • 27
  • 33
12
votes
5 answers

NSURLErrorDomain Code=-1001 error when a http post request is sent

I am using the following code to post an image to my server. @IBAction func postButtonPressed(sender: UIButton) { let task = NSURLSession.sharedSession().dataTaskWithRequest(createRequest("http://xx.xx.xxx.xxx/xxxx/"), completionHandler: { …
Berkan Ercan
  • 1,207
  • 3
  • 14
  • 31
12
votes
3 answers

Swift: How do I return a value within an asynchronous urlsession function?

As you can see, I'm receiving a JSON file, parsing it using SwiftyJSON, and trying to return totalTime, but it won't let me. How do I do this? func googleDuration(origin: String, destination: String) -> Int{ // do calculations origin and…
teo751
  • 321
  • 1
  • 4
  • 13
11
votes
2 answers

Passing headers to URL with Swift URLSession

I don't feel like sharing the actual link as it may have some private information so don't be surprised if it doesn't work. I have a link that looks like this: www.somelink.com/stuff/searchmembers?member=John And some headers that I need to pass,…
Mikara
  • 113
  • 1
  • 1
  • 4
11
votes
3 answers

"The Internet connection appears to be offline" when making URLSession requests on Apple Watch using LTE

Bug: I'm consistently getting error code -1009 "The Internet connection appears to be offline." errors when making URLSession requests in an Apple Watch extension on an Apple Watch Series 3 when connected to the Internet only via LTE. Steps to…
jaredsinclair
  • 12,687
  • 5
  • 35
  • 56