Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to a file Xcode MacOS

I'm unable to run this piece of code which writes the pid to a file.

NSString *text = [NSString stringWithFormat:@"%d\n", getpid()];
NSError *error = nil;

if (![text writeToFile:@"/tmp/Frontend.pid" atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
    NSLog(@"Cannot write PID file %@: %@", @"/tmp/Frontend.pid", error);
    return NO;
}

This is the error I get.

2017-11-13 20:19:18.742171+0530 TestThread[7648:273326] Cannot write PID file /tmp/Frontend.pid: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “Frontend.pid” in the folder “tmp”." UserInfo={NSFilePath=/tmp/Frontend.pid, NSUnderlyingError=0x6040000432a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

I've changed the permission for the directory to 777 and it still doesn't have enough permissions. Please help.

like image 542
alDiablo Avatar asked Apr 13 '26 15:04

alDiablo


2 Answers

Well, I finally fixed it. Here is the default configuration of a new project in xcode which ruined the whole file io thing. I turned off these 2 knobs in the entitlements file and was good to go.

${project_name}.entitlements

like image 183
alDiablo Avatar answered Apr 16 '26 09:04

alDiablo


Just to complement. On XCode10 I changed the entitlements as @alDiablo suggested.

Go to your Target, and on Capabilities, enable the User Selected File as Read/Write.

enter image description here

Thanks!

like image 23
Dx_ Avatar answered Apr 16 '26 11:04

Dx_