Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

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 restart the application, the file does not open. Obviously nothing changes in the code of the application when the app restarts, so the location references remain the same.

The file is being saved to this location:

/var/mobile/Containers/Data/Application/E119DC03-347B-4C84-B07B-C607D40D26B9/Documents/Test_1_Mod.pdf

The odd part is that if I go to the "devices" section of Xcode, I can see the files in the Documents folder, before and after restarting the application:

enter image description here

Edit: Here's how I'm getting the location to save the file:

NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
                                    NSDocumentDirectory,
                                    NSUserDomainMask,
                                    YES);
NSString *path = [arrayPaths objectAtIndex:0];
modified_pdfFileName = [path stringByAppendingPathComponent:modified_filename_pdf];

So, am I saving the file in the wrong location?

Does the file move, somehow during the restart?

Any suggestion for this issue?

Thanks

like image 841
TooManyEduardos Avatar asked Jan 08 '15 19:01

TooManyEduardos


1 Answers

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

like image 114
Brad Brighton Avatar answered Nov 15 '22 05:11

Brad Brighton