Can't find saved file (in device) after restarting the app

前端 未结 1 1447
说谎
说谎 2021-01-17 20:11

I have an app that generates a .pdf file from a picture. If I go read the file in the same session as I took the picture, the file shows up properly.

However, if I r

相关标签:
1条回答
  • 2021-01-17 20:45

    You shouldn't store raw file paths for persistence (or if you do, know that the root can move on you). A better practice would be to only store the relative part of the path and always attach it to the current "root" path in question (particularly if you might be sharing data across devices as with iCloud).

    Still, it is common to take the shortcut, so here's some quick-and-dirty code to take a file path (assumed to be in the Documents directory, you can tweak to taste if you're using a different location) and update it to the current Documents path.

    + (NSString *)rebasePathToCurrentDocumentPath:(NSString *)currentFilePath 
    {
        NSString *fileComponent = [currentFilePath lastPathComponent];
        NSArray *currentDocumentDir = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *currentDocumentPath = [currentDocumentDir objectAtIndex: 0];
        NSString *rebasedFilePath = [currentDocumentPath stringByAppendingPathComponent:fileComponent];
        return rebasedFilePath;
    }
    

    Attach this to a utils class or otherwise integrate it into your flow..

    0 讨论(0)
提交回复
热议问题