Where can a sandboxed Mac app save files?

前端 未结 4 1534
你的背包
你的背包 2021-02-03 23:45

My Mac app is sandboxed and I need to save a file. Where do I save this file? I can\'t seem to find the specific place where this is allowed without using an open panel. This is

4条回答
  •  盖世英雄少女心
    2021-02-04 00:09

    Apple's Sandboxing guide is very useful, found here.

    You basically have a folder dedicated for your app, as described by theAmateurProgrammer in reply to my question here.

    ~/Library/Container/com.yourcompany.yourappname/

    Here is what I have so far, I will improve it later:

    //Create App directory if not exists:
    NSFileManager* fileManager = [[NSFileManager alloc] init];
    NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
    NSArray* urlPaths = [fileManager URLsForDirectory:NSApplicationSupportDirectory 
                                            inDomains:NSUserDomainMask];
    
    NSURL* appDirectory = [[urlPaths objectAtIndex:0] URLByAppendingPathComponent:bundleID isDirectory:YES];
    
    //TODO: handle the error
    if (![fileManager fileExistsAtPath:[appDirectory path]]) {
        [fileManager createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:nil];
    }
    

提交回复
热议问题