iGo*_*iGo 9 macos file-permissions objective-c
从我的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;
}
}];
Run Code Online (Sandbox Code Playgroud)
我需要知道文件写入是如何完成的.静态文件不会写入路径.是否需要指定任何工具名称?SMJobless()可以实现吗?请建议解决方案!!
我在这里发布的另一个解决方案。我浏览了应用程序分发指南。值得注意的是,对于在应用程序商店之外分发的应用程序,不建议使用授权 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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1087 次 |
| 最近记录: |