0

I have a an app in both Android and iOS, the app is supposed to download and save some files on the device with a click of a button. I manage the directories in Android, and I specify the url of the file with this :

Environment.getExternalStorageDirectory().getPath() + "/appDirectory/" + fileName

I wanted to ask what is similar way to specify this in Objective c and iOS.
Right now I use this in objective-c:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask,YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@%@", documentDirectory, @"/myfile.mp4"];

filePath will return something like this:

/var/mobile/Applications/CFEDE74F0A493-499B/Library/Documentation/myfile.mp4


is this the right way?
Thanks very much

m0j1
  • 4,067
  • 8
  • 31
  • 54

1 Answers1

1

For the most part, you've got the right idea. Apple recommends to use NSURL-based file system methods so with that in mind, you can do the following:

NSError *error = nil;
NSURL *documentDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
if (!documentDirectory) {
    // Check error
}
NSURL *fileURL = [documentDirectory URLByAppendingPathComponent:@"myfile.mp4"];

If you need an NSString-based method, you should use built-in method for appending path components instead of using stringWithFormat:

NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *fileURL           = [documentDirectory stringByAppendingPathComponent:@"myfile.mp4"];
dbart
  • 5,468
  • 2
  • 23
  • 19
  • Thanks very much, I used the NSString method and it worked perfectly ;) and also solved one of my other bugs in the code :) – m0j1 Oct 05 '14 at 15:16