获得在 mac 应用程序中将文件写入 \Library\ColorSync\Profiles\ 的权限

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

从我的 mac 应用程序中,我需要将文件写入 \Library\ColorSync\Profiles。为此,该应用程序需要管理员权限才能读写该文件夹。有可能在应用程序中得到相同的吗?任何帮助将不胜感激。

我可以使用以下代码片段弹出权限对话框

 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;
    }
}];

我需要知道如何完成文件写入。静止文件未写入路径。是否需要指定任何工具名称? SMJobless() 有可能吗?求解决方案!!

我可以推荐两种方法。一种方法很快但已弃用。第二种方法是现代的,但需要一些编码和阅读文档:

  1. 您需要编写 bash 脚本,它将执行您需要的操作,并以使用如下方式授予的权限执行它:STPrivilegedTask
  2. 编写特权帮助工具,首次使用root权限访问时将安装一次,您将能够进行任何特权操作How to write privileged helper tool

我在这里发布的另一个解决方案。我浏览了应用程序分发指南。有人提到,对于在应用程序商店之外分发的应用程序,不建议使用授权 APIS。 这个解决方案对我有用的任何方式,它是一个小黑客,我不知道。我尝试 运行 应用程序中的 apple 脚本,它为文件夹(chmod 777 路径)提供完全权限,并在完成我的任务后设置回以前的权限集。

- (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];

}

如果它正在保存您需要的 ColorSync 配置文件,ColorSyncProfileInstall 会为您完成繁重的工作,例如提示用户进行权限和身份验证。 ️

还需要包括 code-signing COLORSYNC_PROFILE_INSTALL_ENTITLEMENT 权利:

<key>com.apple.developer.ColorSync.profile.install</key>
<true/>

上述符号的文档可以在 ColorSyncProfile.h header 中找到——为方便起见,部分复制于此。

CSEXTERN bool ColorSyncProfileInstall(ColorSyncProfileRef profile, CFStringRef domain, CFStringRef subpath, CFErrorRef* error);
   /*
    * profile   - profile to be installed
    * domain    - either kColorSyncProfileComputerDomain or kColorSyncProfileUserDomain.
    *             kColorSyncProfileComputerDomain is for sharing the profiles (from /Library/ColorSync/Profiles).
    *             kColorSyncProfileUserDomain is for user custom profiles (installed under home directory, i.e. in 
    *             ~/Library/ColorSync/Profiles.
    *             NULL is the same as kColorSyncProfileUserDomain.
    * subpath   - CFString created from the file system representation of the path of the file to contain the installed
    *             profile. The last component of the path is interpreted as a file name if it ends with the extension ".icc".
    *             Otherwise, the subpath is interpreted as the directory path and file name will be created from the 
    *             profile description tag, appended with the ".icc" extension.
    * error     - (optional) pointer to the error which will be returned in case of failure.
    *
    *             bool value true is returned if success or false in case of error.
    */