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

What is difference between NSURLSessionDataTask vs NSURLSessionDownloadTask

In latest apple introduce new NSURLSession in replace of NSURLConnection, so in there are different task , so what is the difference between NSURLSessionDataTask, NSURLSessionDownloadTask ? and in which scenario use NSURLSessionTask and where…
35
votes
3 answers

Resume NSUrlSession on iOS10

iOS 10 is going to be released soon so it worth to test applications for compatibility with it. During such test we've discovered that our app can't resume background downloads on iOS10. Code that worked well on previous versions does not work on…
Alexey Guseynov
  • 5,116
  • 1
  • 19
  • 29
29
votes
4 answers

Showing the file download progress with NSURLSessionDataTask

I want to display file download progress (how many bytes are received) of particular file. It works fine with the NSURLSessionDownloadTask .My question is I want to achieve the same with the NSURLSessionDataTask. Here is the code which receives file…
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
5 answers

NSURLSessionTask never calls back after timeout when using background configuration

I am using NSURLSessionDownloadTask with background sessions to achieve all my REST requests. This way I can use the same code without have to think about my application being in background or in foreground. My back-end has been dead for a while,…
18
votes
3 answers

How to resume NSURLSession download process after app force-quit and app relaunch?

I have implemented NSURLSession for downloading fairly large files from our servers. Now as long as I'm working in foreground or background and go back to the app the transactions are working and getting finished. But if I force-quit the app using…
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
17
votes
1 answer

How does one deal with a cancelled NSURLSessionTask in the completion handler block?

If I create a NSURLSessionDownloadTask, and later cancel it before it finishes, the completion block still fires seemingly. let downloadTask = session.downloadTaskWithURL(URL, completionHandler: { location, response, error in ... } How do I…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
16
votes
4 answers

NSURLErrorDomain Code=-997 "Lost connection to background transfer service"

I am using NSURLSession to download some mp3 files and store them in the device. Everything works fine but sometimes out of the blue, the app crashes and i get this weird error saying NSURLErrorDomain Code=-997 "Lost connection to background…
Rashid
  • 762
  • 1
  • 9
  • 30
14
votes
3 answers

Stream video while downloading iOS

I am using iOS 7 and I have a .mp4 video that I need to download in my app. The video is large (~ 1 GB) which is why it is not included as part of the app. I want the user to be able to start watching the video as soon as is starts downloading. I…
13
votes
2 answers

Swift - Downloading video with downloadTaskWithURL

I'm downloading a video thanks to downloadTaskWithURL and I'm saving it to my gallery with this code : func saveVideoBis(fileStringURL:String){ print("saveVideoBis"); let url = NSURL(string: fileStringURL); …
Melanie Journe
  • 1,249
  • 5
  • 16
  • 36
13
votes
1 answer

NSURLSessionDownloadTask downloadTask: didFinishDownloadingToURL file does not exist?

I have implement NSURLSessionDownloadTask for downloading multiple video at a same time. In a normal scenario every thing is working fine. Also background fetch is also working. But when i close the application and restart the application and do the…
Ashwin P
  • 501
  • 1
  • 3
  • 19
13
votes
4 answers

Weird NSURLSessionDownloadTask behavior over cellular (not wifi)

I've enabled Background Modes with remote-notification tasks to download a small file (100kb) in background when the app receives a push notification. I've configured the download Session using NSURLSessionConfiguration *backgroundConfiguration =…
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
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…
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
1
2 3
20 21