NSURLSessionDownloadTask downloadTask: didFinishDownloadingToURL file does not exist?
Asked Answered
P

1

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

Posterity answered 7/8, 2015 at 14:9 Comment(9)
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 *)locationPosterity
can u share the code of saving the file pathEmden
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.trePosterity
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 pathEmden
thats right. But look at the path they are sharing. That doesnt contains any file name.Posterity
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 itSoothsayer
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 namePosterity
@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..Meshwork
@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 #25233886Chromogenic
C
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];
Chlamydeous answered 13/8, 2015 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.