13

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 same downloadTask: didFinishDownloadingToURL the temp file path i am getting is wrong. The file does not exist in the path. When i checked the path through finder i found files are exist the only difference is the path provided doest not contain the file name and also there exist and extra folder with the same name of the parent folder where the files should be stored. Please find the path shared below

/Users/sfm/Library/Developer/CoreSimulator/Devices/EB96B330-4928-422F-8655-DC0E9781014A/data/Containers/Data/Application/54691CE2-D599-41CA-813B-2A8FF7B868F8/Library/Caches/com.apple.nsurlsessiond/Downloads/com.application.tre/com.application.tre

Can any one help to find the path of the downloaded file or handle such scenarios? I did research on finding a solution but sorry to say this couldn't find it any where

Ashwin P
  • 501
  • 1
  • 3
  • 19
  • I am trying to download multiple video at a time. if the application is closed and opened and follow the same thing one more time the download the location os wrongly shared in the download task delegate method - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location – Ashwin P Aug 10 '15 at 14:03
  • can u share the code of saving the file path – Mukesh Aug 10 '15 at 14:05
  • The problem is the temporary file path shared from the delegate itself is wrong. The path shared is: /Users/sfm/Library/Developer/CoreSimulator/Devices/EB96B330-4928-422F-8655-DC0E9781014A/data/Containers/Data/Application/54691CE2-D599-41CA-813B-2A8FF7B868F8/Library/Caches/com.apple.nsurlsessiond/Downloads/com.application.tre/com.application.tre – Ashwin P Aug 10 '15 at 14:07
  • 1
    i am asking for code.the method where u r saving the downloaded video `didFinishDownloadingToURL` .Are u saving the video.u cant reuse the temp path – Mukesh Aug 10 '15 at 14:16
  • thats right. But look at the path they are sharing. That doesnt contains any file name. – Ashwin P Aug 10 '15 at 14:21
  • Make sure you are checking the file at the location synchronously with the call. NSURLSession will remove the response right after the completion handler is called. Its your responsibility to move it – Daniel Galasko Aug 11 '15 at 07:34
  • I am checking that and also i am moving the file inside the delegate itself. Please read my question. This works fine if the user quitting for 1 time and re opening it. the second time he quits and restart it. download process will be running and on success the location path provide will not even contain the file name – Ashwin P Aug 11 '15 at 09:26
  • @Ashwin Can you put your code, what you are using, I guess you are creating Temp Path for each new Download, as after you start app, you must be restarting download with new temp path, where you should be using the last temp path where your download first started and Resume download.. – iphonic Aug 17 '15 at 07:48
  • @Ashwin you can't use full path anymore from iOS 8 onwards, check this in iOS 7 it will work fine. Just save file name and create path every time. This link might help you http://stackoverflow.com/questions/25233386/xcode-6-keeps-renaming-my-apps-directory-in-ios8-simulator-after-each-run – Nikhil.T Aug 18 '15 at 18:09

1 Answers1

2

When debugging an app, since iOS8, file paths change every time you start a debug session (at least in the Simulator).

Check the app-identifier in the path, it sometimes changes when you debug it.

Since iOS8, you should use this for fileURLs:

NSURL *documentsPath = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];

In setDownloadTaskDidFinishDownloadingBlock: I use this to return the fileURL where it should save the file:

return [documentsPath URLByAppendingPathComponent:fileName];

To open the file:

NSURL *fileURL = [documentsPath URLByAppendingPathComponent:fileName];

To delete the file:

NSURL *fileURL = [documentsPath URLByAppendingPathComponent:fileName];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];
peterh
  • 11,875
  • 18
  • 85
  • 108
dOM
  • 555
  • 5
  • 14