我有一个OSX xcode项目,在我打开沙盒之前一直运行良好。
在功能下,我启用了Sandbox和文件访问权限: 用户选择的文件 - 读/写
我的项目是这样的,当用户点击按钮时,NSSavePanel将弹出,用户指定保存目录和文件名(即myfile.dmg),一旦他或她点击保存,程序将创建使用NSTask和hdiutil到特定路径的dmg文件。这是我的代码:
__block NSSavePanel *saveDlg = [NSSavePanel savePanel];
// Enable options in the dialog.
[saveDlg setCanCreateDirectories:YES];
[saveDlg setAllowedFileTypes:[NSArray arrayWithObject:@"dmg"]];
[saveDlg setCanSelectHiddenExtension:YES];
[saveDlg beginSheetModalForWindow:self.view.window completionHandler:^ (NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
BOOL bSuccess = [self checkErrorHandler: saveDlg.URL optionToCheck: 8];
if (bSuccess) {
[self createDMG:saveDlg.URL];
}
}
saveDlg = nil;
}];
这是我的createDMG方法:
- (void)createDMG:(NSURL *)myurl{
self.url = myurl;
self.createTask = [[NSTask alloc] init];
NSPipe *outputPipe = [NSPipe pipe];
NSPipe *errorPipe = [NSPipe pipe];
NSPipe *writePipe = [NSPipe pipe];
self.fhCreateOutput = [outputPipe fileHandleForReading];
self.fhCreateError = [errorPipe fileHandleForReading];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(createNotifiedForStdOutput:) name:NSFileHandleReadCompletionNotification object:self.fhCreateOutput];
[nc addObserver:self selector:@selector(createNotifiedForStdError:) name:NSFileHandleReadCompletionNotification object:self.fhCreateError];
[nc addObserver:self selector:@selector(createNotifiedForComplete:) name:NSTaskDidTerminateNotification object:self.createTask];
NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
NSString *pw = [self getPlainTextPassword:nil];
[writeHandle writeData: [[NSString stringWithFormat:@"%@\0\n", pw] dataUsingEncoding:NSUTF8StringEncoding]];
[writeHandle closeFile];
[self.createTask setStandardInput:writePipe];
NSString *directoryPath = [self.url.path stringByDeletingLastPathComponent];
NSString *fileName = [[self.url absoluteString] lastPathComponent];
[self.createTask setLaunchPath: @"/usr/bin/hdiutil"];
[self.createTask setCurrentDirectoryPath:directoryPath];
[self.createTask setArguments:[NSArray arrayWithObjects:@"create", @"-puppetstrings", @"-encryption",@"AES-128", @"-size", @"100m", @"-verbose", @"-volname", @"test", fileName, @"-fs", @"HFS+J", @"-stdinpass", nil]];
[self.createTask setStandardOutput: outputPipe];
[self.createTask setStandardError:errorPipe];
[self.createTask launch];
[self.fhCreateOutput readInBackgroundAndNotify];
[self.fhCreateError readInBackgroundAndNotify];
}
我收到以下错误:
2015-02-18 10:51:47.374 My DMG[41725:2016417] -[MyDMG
createNotifiedForStdError:] verbose: 2015-02-18 10:51:46.745 diskimages-helper[41741:2016793] ERROR: couldn't connect to framework.
2015-02-18 10:51:47.376 My DMG[41725:2016417] -[MyDMG createNotifiedForStdError:] verbose: 2015-02-18 10:51:46.745 diskimages-helper[41741:2016793] DIHelper: setupConnectionToFrameworkWithUUID: failed
2015-02-18 10:51:46.747 hdiutil[41739:2016783] helper died
DIHLDiskImageCreate() returned 10
(null)
2015-02-18 10:51:47.665 My DMG[41725:2016417] -[MyDMG createNotifiedForStdError:] verbose: hdiutil: create: returning 10
2015-02-18 10:51:47.682 My DMG[41725:2016417] -[MyDMG createNotifiedForStdError:] verbose: hdiutil: create failed - No child processes
2015-02-18 10:51:47.687 My DMG[41725:2016417] task completed or was stopped with exit code 1
有些人说NSTask对沙箱不起作用,但是我还有一些其他项目在沙盒中运行NSTask并且它们有效。另外,如果你看一下mac app store,有一个类似的app创建dmg(用户拖动文件夹和创建dmg文件)
感谢任何帮助:)