我在一个应用程序组中有2个沙盒应用程序,并尝试将在#34; Main"(代码中的SandboxTest)应用程序中生成的文件传输到" Helper"(代码中为SandboxPair) )申请。使用沙箱它在10.11上工作,在10.12 Sierra上失败。当沙箱关闭时也可以正常工作。
Helper是由Main使用NSTask启动的(我也尝试过NSWorkspace,结果相同)。以下是我启动它并发送文件的方式:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//Testing
_path = [[NSBundle mainBundle] pathForResource:@"SandboxPair" ofType:@"app"];
_path = [_path stringByAppendingPathComponent:@"/Contents/MacOS/SandboxPair"];
if( ![self runTaskWithLaunchPath:_path] ) {
NSLog(@"launching helper app failed");
}
[self performSelector:@selector(sendFile) withObject:nil afterDelay:2];
return;
}
- (BOOL)runTaskWithLaunchPath:(NSString*)launchPath
{
_helperAppTask = [NSTask new];
if( ![[NSFileManager defaultManager] fileExistsAtPath:launchPath] )
{
NSLog(@"ERROR: Not found command %@", launchPath);
return NO;
}
_helperAppTask.launchPath = launchPath;
NSPipe* outPipe = [NSPipe pipe];
NSPipe* errPipe = [NSPipe pipe];
_helperAppTask.standardOutput = outPipe;
_helperAppTask.standardError = errPipe;
[_helperAppTask launch];
return YES;
}
///Run openFile:withApplication: test
- (void)sendFile
{
NSString* groupIdentifier = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppIdentifierPrefix"]
stringByAppendingString:[[NSBundle mainBundle]
objectForInfoDictionaryKey:(NSString*)kCFBundleIdentifierKey]];
NSURL* sharedPath = [[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:groupIdentifier];
NSError* error = nil;
[[NSFileManager defaultManager] createDirectoryAtURL:sharedPath
withIntermediateDirectories:YES attributes:nil error:&error];
NSString* fileName = @"test.txt";
fileName = [[@"~/Downloads/" stringByAppendingPathComponent:fileName]stringByExpandingTildeInPath];
[@"test file" writeToFile:fileName atomically:YES
encoding:NSUTF8StringEncoding error:&error];
NSLog(@"File written at path: %@", fileName);
BOOL opened = [[NSWorkspace sharedWorkspace] openFile:fileName withApplication:_path];
if(!opened) {
NSLog(@"File was not opened in helper application");
} else {
NSLog(@"File was successfully opened in helper application");
}
}
来自帮助应用程序的AppDelegate.m的接收文件代码:
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
NSAlert* alert = [[NSAlert alloc] init];
alert.messageText = @"Helper app: File was recieved. application:openFile: is working";
[alert addButtonWithTitle:@"OK"];
[alert runModal];
return YES;
}
- (void)application:(NSApplication *)sender openFiles:(NSArray<NSString *> *)filenames
{
NSAlert* alert = [[NSAlert alloc] init];
alert.messageText = @"Helper app: Files was recieved. application:openFiles: is working";
[alert addButtonWithTitle:@"OK"];
[alert runModal];
}
您可以在github上看到测试项目:https://github.com/pogosskiy/SierraSandbox 还有一个代码来测试这些进程之间的NSConnection。我试图用它来表示应用程序组Group中有一个文件,但它在沙箱下的10.12也失败了。
我该如何解决这个项目?也许权利中有一些沙箱标志?我已经在com.apple.security.temporary-exception.apple-events中尝试了一些,但没有效果(或者可能有错误)。