Search code examples
macoscocoasandbox

make folder in sand box of osx


using the code below to get the file path:

NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:executableName];

then I want to save data to the file (settings.bundle)

NSString *filePath = [path stringByAppendingPathComponent:@"settings.bundle"];
[someString writeToFile:settingsPath atomically:YES encoding:NSUTF8StringEncoding error:&error];

the error:

Error Domain=NSCocoaErrorDomain Code=4 "The folder “settings.bundle” doesn’t exist." UserInfo=0x7ff8894cecc0 {NSFilePath=/Users/xxxx/Library/Application Support/AppName/settings.bundle, NSUserStringVariant=Folder, NSUnderlyingError=0x7ff8894a1750 "The operation couldn’t be completed. No such file or directory"}

but if I mkdir AppName manual there is no error:

 mkdir /Users/xxxx/Library/Application Support/AppName

So My question is : Cocoa can NOT create folder automaticly in osx?


Solution

  • Give this a shot: [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];

    That method will create not only the directory at the specified path, but also any intermediate ones that may not exist.