9

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

TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
  • 1
    Restart the application from the springboard or restart the application in Xcode? Try this: 1) Run the application in Xcode, then stop it. 2) Run the application by tapping the icon in the springboard. Is the file still found? – Brad Brighton Jan 08 '15 at 19:58
  • Sorry I should've clarified that. When I restart the application from Xcode, or close the application in the device (swipe the app up, and reopen it) the file cannot be opened (or its missing). Restart from springboard works fine since the application never exits. – TooManyEduardos Jan 08 '15 at 20:00
  • In either case the file is visible under "Devices" in xcode, but the code doesn't open the file. Am I saving it on a temp location? – TooManyEduardos Jan 08 '15 at 20:01
  • 1
    If you post the code you use to determine the path to the file, it would be easier to confirm, but it sounds like you're storing a string path rather than letting the OS tell you where to start looking (NSSearchPathForDirectoriesInDomains) and yes, part of that string path can vary on you (the app UUID). – Brad Brighton Jan 08 '15 at 20:04
  • I just posted the code of how I'm getting the location of the when I'm saving it – TooManyEduardos Jan 08 '15 at 20:06
  • Saving isn't your problem (based on your problem description) - it's loading. Where are you getting the path for that? – Brad Brighton Jan 08 '15 at 20:07
  • So the location where the file is getting saved is getting stored in core data, so when I open the file I just ask core data to give me the location where the file got stored (which is a string that comes from that code I just posted) – TooManyEduardos Jan 08 '15 at 20:08
  • Then yes, part of the path string you're storing can change on you from build to build. I have some code lying around that can rebase paths... lemme dig it up. – Brad Brighton Jan 08 '15 at 20:12
  • Thanks. Put it as an answer so I can mark it answered please. – TooManyEduardos Jan 08 '15 at 20:12
  • https://stackoverflow.com/a/53274341/945906 – BB9z Nov 13 '18 at 05:34

1 Answers1

17

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

Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
Brad Brighton
  • 2,179
  • 1
  • 13
  • 15
  • so your suggestion is to have the same process of saving for opening, just pass the "test1.pdf" parameter – TooManyEduardos Jan 08 '15 at 20:30
  • 1
    Correct. If you know that your dirTree starts in Documents, only store the parts you need _below_ that. If you're not structuring your storage (that is, the files are all at the "root" of Documents), then all you need is the filename and let the system tell the Documents path details at runtime. – Brad Brighton Jan 08 '15 at 20:32