Sandboxed app & NSOpenPanel causes crashes

2019-04-04 07:58发布

问题:

I am doing a simple file open panel in my Cocoa app. I enable entitlements and app sandboxing. But on OS X 10.9, when the app should open a dialog using NSOpenPanel, it crashes with:

Application Specific Information:
Terminating app due to uncaught exception 'NSObjectNotAvailableException', reason: 'service com.apple.appkit.xpc.openAndSavePanelService failed to init an instance of NSOpenPanelService on behalf of due to { "proxy-communications-error" = "Error Domain=NSCocoaErrorDomain Code=4099 \"\U041d\U0435 \U0443\U0434\U0430\U043b\U043e\U0441\U044c \U0443\U0441\U0442\U0430\U043d\U043e\U0432\U0438\U0442\U044c \U0441\U0432\U044f\U0437\U044c \U0441 \U043f\U0440\U043e\U0433\U0440\U0430\U043c\U043c\U043e\U0439-\U043f\U043e\U043c\U043e\U0449\U043d\U0438\U043a\U043e\U043c.\" (The connection was invalidated from this process.) UserInfo=0x61000047dc00 {NSDebugDescription=The connection was invalidated from this process.}"; }'

Source:

self.panel = [NSOpenPanel openPanel];
panel.delegate = self;
panel.canChooseDirectories = YES;
panel.canChooseFiles = NO;
panel.canCreateDirectories = YES;
panel.allowsMultipleSelection = NO;
[self.panel beginSheetModalForWindow:contextWindow completionHandler:^(NSInteger returnCode) {
        ...
    }];

Has anyone seen this before?

回答1:

Indeed, you have to specifically allow Read/Write permissions on User Selected Files. In Xcode 5.1 this is under Capabilities - App Sandbox.



回答2:

I think that you need to activate "User selected file" in your app entitlements!

Give it a try, in xcode 5 beta looks like this, in xcode 4 should be on the general page of your project, where you activate entitlements!

let me know!

--------- Edit

Well, i think your problem is in how you call the panel.

First, initialize the panel with:

NSOpenPanel * openDlg = [NSOpenPanel openPanel];

[openDlg setCanChooseFiles:NO];
[openDlg setAllowsMultipleSelection:NO];
[openDlg setCanChooseDirectories:YES];
[openDlg setCanCreateDirectories:YES];

finally, change the dialog "call" from:

[self.panel beginSheetModalForWindow:contextWindow completionHandler:^(NSInteger returnCode) { ... }]; 

to:

if ([openDlg runModal] == NSOKButton) 
{ here you manage the user choice. } 

It should work now!



回答3:

My OS X app "Make a Face," also has problems when attempting to call a simple file open panel. The app does not crash, but the "select file to open" window starts shimmering, the pixel light in the dialogue box starts throbbing, and mouse clicks have a delayed impact from anywhere between 20 to 40 seconds. Very surreal. This only happens in Mavericks 10.9.

Sandbox entitlement "com.apple.security.files.user-selected.read-write" is enabled, as it has been for all previous uploads of the app. However, under Mavericks, the "throbbing light file open panel syndrome" makes opening a user selected JPEG or PNG file from within the app a very bumpy ride.



回答4:

In my case the problem for this error was calling [NSOpenPanel openPanel] on a thread that was not the main one. Encapsulating the whole thing inside a

dispatch_async(dispatch_get_main_queue(), ^{
   NSSavePanel *saveAsPanel = [[NSSavePanel alloc] init];
   // ... bla bla...
 });

solved the problem