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

时间:2016-01-11 08:52:20

标签: objective-c macos file-permissions

从我的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()可以实现吗?请建议解决方案!!

3 个答案:

答案 0 :(得分:2)

我在这里发布的另一个解决方案。我浏览了应用程序分发指南。有人提到,不建议将授权APIS用于分布在应用商店一侧的应用程序。 无论如何这个解决方案对我有用,这是一个小小的黑客,我不知道。我尝试从应用程序运行苹果脚本,它为文件夹(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];

}

答案 1 :(得分:1)

我可以建议2种方法。一种方法很快但不赞成使用。第二种方法是现代的,但需要一些编码和阅读文档:

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

答案 2 :(得分:1)

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

还必须包括代码签名COLORSYNC_PROFILE_INSTALL_ENTITLEMENT权利:

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

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

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.
    */