gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:29:34 25 4
gpt4 key购买 nike

在我的 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() 有可能吗?请提出解决方案!!

最佳答案

我在这里发布的另一个解决方案。我浏览了应用程序分发指南。有人提到,对于在应用程序商店之外分发的应用程序,不建议使用授权 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];

}

关于objective-c - 获得在 mac 应用程序中将文件写入\Library\ColorSync\Profiles\的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34717355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com