Oz0234
Oz0234

Reputation: 148

FileURL gets invalid each time I rebuild the application or install a new version

I'm saving videos and audio files I'm recording in the application I'm working on. Here's an example of a URL part:

@"file:///var/mobile/Containers/Data/Application/1AADB360-ABA7-493E-AE35-290803AA2EBD/Documents/444571753026.caf"

If I rebuild or install a new version from testflight, the application doesn't find the file, and if I save another one, the bold part of the URL has changed. How do I handle this? Keep in mind I already have a version of this app on the appstore and it would be preferred if I could update the app without the users losing their data.

Upvotes: 0

Views: 39

Answers (1)

Numan Tariq
Numan Tariq

Reputation: 1296

That identifier can change and there are no guarantees that it will always remain the same. iOS SDK provides relevant API's so that you do not have to depend on that and always get the correct path to the Documents Folder. You can use:

NSURL *directoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

This will give you the URL for the documents directory for your app. For further exploration inside the directory:

NSFileManager *fm = [NSFileManager defaultManager];
NSDirectoryEnumerator *enumerator = [fm enumeratorAtURL:directoryURL includingPropertiesForKeys:@[ NSURLNameKey, NSURLIsDirectoryKey ] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants|NSDirectoryEnumerationSkipsHiddenFiles errorHandler:NULL];

In case you know the file or folder's name, you can use:

NSURL *fileURL = [directoryURL URLByAppendingPathComponent:directoryName isDirectory:NO];

You can configure the enumerator according to your requirements to perform as you desire. This is only a sample that I am using in one of my apps.

You can replace NSDocumentDirectory with NSCachesDirectory or NSLibraryDirectory if you require access to those.

Upvotes: 1

Related Questions