Search code examples
objective-cmacosfile-permissions

Getting privilege to write a file to \Library\ColorSync\Profiles\ in a mac application


From my mac application, I need to write a file to \Library\ColorSync\Profiles. For this, the app needs admin privilege to read write to the folder. Is possible to get the same with in the application? Any help will be appreciated.

I am able to pop up the permission dialog with the following code snippet

 NSSavePanel *tvarNSSavePanelObj    = [NSSavePanel savePanel];
[tvarNSSavePanelObj setDirectoryURL:[NSURL URLWithString:@"/Library/ColorSync/Profiles"]];

__block NSString *filePathexn  = nil;

[tvarNSSavePanelObj beginSheetModalForWindow:[NSApplication sharedApplication].mainWindow completionHandler:^(NSInteger tvarInt) {
    if(tvarInt == NSModalResponseOK){
        NSURL* tvarUrl = [tvarNSSavePanelObj URL];
        NSLog(@"doSaveAs filename = %@",[tvarUrl path]);
        NSString *filePath = [tvarUrl path];
        filePathexn = [filePath stringByAppendingPathExtension:@"rtf"];
        OSStatus status;
        if(![[NSFileManager defaultManager]isWritableFileAtPath:filePath]){
            NSLog(@"Not Writable at path");
            AuthorizationRef authRef;
            AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
            AuthorizationRights rights = {1, &right};
            status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagPreAuthorize, &authRef);
            AuthorizationFlags authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize;
            status = AuthorizationCopyRights(authRef, &rights, kAuthorizationEmptyEnvironment, authFlags, NULL);
        }
       BOOL status1 = [[NSFileManager defaultManager]createFileAtPath:filePathexn contents:nil attributes:nil];

    } else if(tvarInt == NSModalResponseCancel) {
        return;
    } else {
        NSLog(@"doSave As tvarInt not equal 1 or zero = %3ld",(long)tvarInt);
        return;
    }
}];

I need to know how the file write could be done. Still file is not written to the path. Is any tool name need to be specified? Will it be possible with SMJobless() ? Kindly advise a solution!!


Solution

  • Another solution I am posting here. I went through the app distribution guide. It is mentioned that the Authorization APIS are not recommended for an app that is distributed out side of app store. Any way this solution is working for me, it is a little hack, I don't know. I try to run the apple script form the application, which gives full privilege to the folder (chmod 777 path) and after finishing my task set back to previous privilege set.

    - (void)runapplescript{
    NSDictionary* errorDict;
    NSAppleEventDescriptor* returnDescriptor = NULL;
    NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource:
                                   @"do shell script \"chmod 777 /Library/ColorSync/Profiles\" with administrator privileges"];
    returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
    
    }