Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 223
Ashwin P Avatar asked Aug 07 '15 14:08

Ashwin P


1 Answers

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];
like image 127
dOM Avatar answered Sep 23 '22 03:09

dOM