Cannot find the saved file (on the device) after restarting the application.

I have an application that creates a .pdf file from a drawing. If I read the file in the same session as I took the picture, the file will appear correctly.

However, if I restart the application, the file does not open. Obviously, nothing changes in the application code when the application restarts, so the links to the location remain unchanged.

The file is saved in this place:

/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 Xcode “device” section, I can see the files in the “Documents” folder before and after restarting the application:

enter image description here

Edit: Here, as I get 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 place?

Does the file move during reboot?

Any suggestion on this issue?

thanks

+7
ios
source share
1 answer

You cannot store raw file paths to save (or, if so, be aware that the root can move on you). It would be best practice to save only the relative part of the path and always attach it to the current “root” path in question (in particular, if you can share data between devices, as with iCloud).

Nevertheless, it is customary to use a shortcut, so here you can find some quick and dirty code to take the path to the file (it is assumed that it is in the Documents directory, you can configure it if you use another location) and update it to the current path to documents.

 + (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 the utils class or otherwise integrate it into the stream.

+14
source share

All Articles