Is it possible to save a file in a custom folder inside NSDocumentDirectory

1.3k views Asked by At


Is it possible to save a file (any type) inside a custom folder in NSDocumentDirectory? I mean every time whatever I try to save I use this as resource file path:

NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], fileName, nil];
outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

or

NSString *imageDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

And as a result it always save in the following directory,
~/Library/Application Support/iPhone Simulator/......./Documents/file.ext

But is it possible to save that file inside a custom folder like this:
~/Library/Application Support/iPhone Simulator/......./Documents/CustomFolder/file.ext.

If yes, Could you tell me details about the process?
Thanks in advance.
Have a good day.

Additional:

Here, Actually I save an audio voice record file. The name of that file came from current time. I want to save that voice record file in a custom folder.

Here is my code:

- (NSString *) dateString
{
    // return a formatted string for a file name
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"ddMMMYYY_hh:mm:ssa";
    return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".m4a"];
}

// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], fileName, nil];
outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

In outputFileURL I save my recorded voice.

I try with this :

// Set the audio file
NSString *customDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
customDirectoryPath = [customDirectoryPath stringByAppendingPathComponent:@"MyCustomDirectory"];

NSArray *pathComponents = [NSArray arrayWithObjects:customDirectoryPath, fileName, nil];
    NSLog(@"pathComponents %@", pathComponents);
    outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
    NSLog(@"outputFileURL %@", outputFileURL);

The folder is created with out any problem but the audio file is not save in that folder. Thanks.

2

There are 2 answers

6
Rameswar Prasad On BEST ANSWER

Yes it is possible. You just need to create a folder inside document directory.

- (NSURL *)audioRecordingPath:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"/Recorded"];
    NSFileManager *fileManager  = [NSFileManager defaultManager];

    NSError *error = nil;
    if (![fileManager fileExistsAtPath:folderPath])
        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

    NSString *filePath = [folderPath stringByAppendingPathComponent:fileName];

    return [NSURL fileURLWithPath:filePath];
}

Then fetch the folder everytime you want to store something and store into it.

6
Puran On

Yes it's possible, you can just append the directory name after NSDocumentDirectory and then may be do something like

 NSString *customDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 customDirectoryPath = [customDirectoryPath stringByAppendingPathComponent:@"MyCustomDirectory"]
 if (![[NSFileManager defaultManager] fileExistsAtPath:customDirectoryPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:customDirectoryPath
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:&error];