Posts Tagged ‘iOS’

 

App Dev Tips – App directories change after every update

July 8, 2011
Share

I learned this the hard way and wanted to share my experience.

Every time a user updates your app from the App Store, the default document directory changes. The best practice in this case would be to save a relative path to your files instead of the fully-qualified path. If you do this, the only thing you have to worry about is getting the current app document directory and appending the path you have saved in previous versions.

The way I have done this is by implementing 2 helper methods as follows:

- (NSString *)applicationDocumentsDirectory {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;

}

- (NSString *) trueUrl{

NSString *directory = [[self applicationDocumentsDirectory] stringByAppendingString:_relativeDirectory];
return directory;

}

So say for example you have created an images directory and saved a PNG image into that directory. You will have a file path such as:

YourAppDocumentDirectory1.0/images/file.png

When your users upgrade to version 1.1, the new path for the previously saved image will change:

YourAppDocumentDirectory1.1/images/file.png

Since you will not know beforehand what this directory will be, you can use the aforementioned helper methods to do the job for you:

- (NSString *)applicationDocumentsDirectory;

will return your current application document directory:

YourAppDocumentDirectory1.0 or YourAppDocumentDirectory1.1

and

- (NSString *) trueUrl;

will take the relative path you have stored

/images/file.png

and append it to the current application directory, thus giving you the true path you wanted in the first place.

If you have any questions don’t hesitate to ask.