Questions tagged [nsurlsessiondownloadtask]

An NSURLSessionDataTask is a concrete subclass of NSURLSessionTask. The methods in the NSURLSessionDataTask class are documented in NSURLSessionTask Class Reference.

An NSURLSessionDataTask behaves differently from other subclasses in several subtle ways:

  • An NSURLSessionDataTask object returns data directly to the app rather than through a file (as a download task would).
  • During upload of the body data (if your app provides any), the session periodically calls the delegate’s URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend: method with status information. (It also calls this method for upload tasks.) Upon receiving an initial response, the session calls the delegate’s URLSession:dataTask:didReceiveResponse:completionHandler: method to provide your app with the opportunity to convert the transfer into a download task depending on the returned MIME type and other information.

  • If your app converts the transfer into a download task, the session calls the delegate’s URLSession:dataTask:didBecomeDownloadTask: method to provide your app with the new download task.

  • During the transfer, the session calls its delegate’s URLSession:dataTask:didReceiveData: method with data objects containing bits of data as it is received. Upon completion, the session calls its delegate’s URLSession:dataTask:willCacheResponse:completionHandler: method to determine whether the response should be cached.

  • If this delegate method is provided, it must call the provided completion routine; otherwise, your app leaks memory.

  • If this delegate method is not provided, the default behavior is to use the caching policy specified in the session’s configuration object.

  • Unlike download tasks, requests made through NSURLSessionDataTask objects cannot be resumed after they are canceled.

309 questions
4
votes
2 answers

Why does my URLDownloadTask not call didFinishDownloadtingTo?

Most of my programming experience is in Shell and Python. I'm fairly new to Swift, like "3 days ago" new. I just can't figure out why didFinishDownloadtingTo is not called when my downloadTask completes. Here's my AppDelegate.swift file: import…
Matt
  • 65
  • 7
4
votes
1 answer

ResumeData getting nil when using downloadTaskWithRequest

If I'm using this below code aDownloadTask = [self.backgroundSession downloadTaskWithURL:aRemoteURL]; Desc of above code: When I started download by using downloadTaskWithURL It’s return expectedContentLength negative(-1) But that time resume data…
4
votes
0 answers

How to resume download in swift?

I'm a freshman in Swift and working on an app which would resume download after being closed. I found some similar questions from Stack overflow but they are not very helpful to my work. let dataTask =…
Joey Zhang
  • 363
  • 6
  • 18
4
votes
1 answer

NSURLSessionDownloadTask issues with Storage almost full disk warnings

I'm having issues with handling "out of space" / "full disk" errors on ios with NSURLSessionDownloadTask If the disk is full due to downloads done in the app I get a call to URLSession(session: NSURLSession, task: NSURLSessionTask,…
Daniela
  • 71
  • 5
4
votes
0 answers

NSURLSessionResponseBecomeDownload and NSURLSessionDownloadTask delegate issue

In my project, I start a NSURLSessionDataTask to check the header of a request, and if they're right, I turn the request into a download or I just discard it: - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask…
entropid
  • 6,130
  • 5
  • 32
  • 45
4
votes
1 answer

How to change the URL when using NSURLSession downloadTaskWithResumeData:?

I use NSURLSession downloadTaskWithURL: to download a file, and use NSURLSessionDownloadTask cancelByProducingResumeData: to produce an NSData and save it to a local temp file. Then I want to resume the download task by using NSURLSession…
Amztion
  • 41
  • 5
4
votes
1 answer

Download a file with NSURLSession in Swift

i have like 2 problems here , first i cant set NSURLSessionDownloadDelegate with a swift project, compiler says Type 'ViewController' does not conform to protocol 'NSURLSessionDownloadDelegate' Second problem is i cant find NSURLSession methods to…
Amr Mohamed
  • 2,290
  • 4
  • 20
  • 39
4
votes
1 answer

What's the difference between downloadtaskwithrequest and downloadtaskwithurl

I'm watching the Stanford CS193p lecture video. The instructor used both downloadTaskWithRequest: and downloadTaskWithURL: when downloading some photos from Flickr. I'm a bit confused. I looked up the former in the documentation, which speaks of…
4
votes
2 answers

Several DownloadTasks sequentially

I have n tasks sending to a server. When I launch them by [taskN resume] they will launch parallelly but i want to launch them sequentially - if first task finish, other task will launch. Any suggestions?
Patrik Vaberer
  • 646
  • 6
  • 15
4
votes
2 answers

NSURLSession background task: avoid duplicates

I am new to NSURLSession and specially background download tasks. Is there a way to query NSURLSession to return a list of background tasks? It seems possible to make duplicate requests for background download tasks. I do not get any error if I make…
NSRover
  • 932
  • 1
  • 12
  • 29
4
votes
2 answers

NSURLSession backgroundSession additional API call on completion

I need to upload content to AWS S3 via PUT that can be run in a background session using NSURLSessionUploadTask. So far it works great. However I need to then call my API once the upload to S3 has finished to change it's state to complete. I AWSS3…
4
votes
0 answers

Dealing with incomplete data using NSURLSessionDataTask

I'm using NSURLSessionDataTask to download data. I'm seeing cases in which the connection is interrupted and yet I still receive data. How should I deal with these cases? Am I guarranted to get an error of some kind? Should I compare the data…
hpique
  • 119,096
  • 131
  • 338
  • 476
4
votes
1 answer

How do I get the data from a finished `NSURLSessionDataTask`?

I know I can use dataTaskWithURL:completionHandler: to get the data in the completionHandler block, but that blocks the delegate methods from firing, and I need the didReceiveData: method to fire, as it's how I configure my progress indicator. I'm…
user2005643
3
votes
0 answers

WatchOS Background URLSession handle(_:) not called when app is not closed (lowered wrist)

I'm trying to create a Watch-App where I download data (around 30 MB). Therefore I create a URLSession with a background configuration like so: let config = URLSessionConfiguration.background(withIdentifier: "") self.session =…
3
votes
2 answers

URLSessionConfiguration Background download task fails when device is locked with error - Lost connection to background transfer service

I'm getting a Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service" when I lock my screen after starting a background download process. I'm using Alamofire but I assume this would happen on a regular URLSession…
newDeveloper
  • 1,365
  • 1
  • 17
  • 27