Home > OS >  Why is the path of NSApplicationSupportDirectory not the same in Cocoa app and console app?
Why is the path of NSApplicationSupportDirectory not the same in Cocoa app and console app?

Time:10-13

I made a small tool and needed to access the Application Support directory of the user layer, so I created a command line project, used URLsForDirectory to get the path, and everything worked fine. But when I create a cocoa project with a gui, the path it returns is under the Containers directory, which doesn't seem to exist. What's causing this discrepancy? What should I do to get the ~/Library/Application Support directory in the cocoa project?

this is the code:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    NSArray* pathes = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask
    ];
    NSString* applicationSupportPath = [pathes firstObject];
    NSLog(@"Application Support:%@\n", applicationSupportPath);
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end

the output is :

2022-10-03 22:01:23.230600 0800 TestApplicationSupportPath[49579:213899] Application Support:file:///Users/bodong/Library/Containers/com.bodong.TestApplicationSupportPath/Data/Library/Application Support/

console :

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
    }
    
    NSArray* pathes = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask
    ];
    NSString* applicationSupportPath = [pathes firstObject];
    NSLog(@"Application Support:%@\n", applicationSupportPath);
    
    return 0;
}

the output is :

2022-10-03 22:02:09.341780 0800 TestASPCmd[49791:215344] Application Support:file:///Users/bodong/Library/Application Support/
Program ended with exit code: 0

CodePudding user response:

I found a solution, just in the project properties, switch to Signing&Capabilities, delete the Sanbox group.

  • Related