1
votes

I'm trying to save a downloaded file so I can open it in another session. I'm saving the mp3 data to the documents directory, and I'm saving the url to the file in a local datastore. When I check using

        if ([[NSFileManager defaultManager] fileExistsAtPath:musicObject[@"localFile"]]){
        NSLog(@"applicationDocumentsDir exists");
        }
    else {
        NSLog(@"File doesn't exist");
    }

it returns "File doesn't exist", but I know it does because I've printed out the documents directory which gives me

"file:///private/var/mobile/Containers/Data/Application/94552DFC-022B-4962-9CB7-CCD87CB43E57/Documents/xDDsCbXAFhwEqGIzJfJRByEr1.mp3",

and I'm trying to access it with the same path but the first is private. How do I make the file not private (I have saved it earlier in the app)

file:///var/mobile/Containers/Data/Application/AE27BD8F-5EEB-48FC-A8D4-E228F99CECE3/Documents/xDDsCbXAFhwEqGIzJfJRByEr1.mp3

1
Never store the full path to a file since the path can change. Only store the filename relative to the Documents folder and calculate the full path each time your app runs. - rmaddy

1 Answers

1
votes

I suggest the following steps:

  1. Take the last path component (that's just your mp3 filename)
  2. Get the current Documents directory
  3. Build a new path with the current Documents directory and the extracted mp3 filename

If I remember correctly, /var is a symlink to /private/var. So depending on how the path is built, one may end up with one or the other.

I inherited a project which was struck by the same problem, only with an extra randomly named directory in between. Eventually I removed the leading /private component, constructed an array of path components and checked, whether replacing non-existent elements with the current value leads to an existing file. You case should be easier to handle.

I don't remember when, but at some point the application directory (the hex numbers path component) began to change with almost each run in the simulator. Beginning with this behaviour such problems became much more visible. Although one should not save full paths, I suspect a lot of projects didn't care in the past. On one hand because things just worked, and on the other hand because a lot of people just don't know it.