macosnsdocumentcontroller

NSDocumentController openDocumentWithContentsOfURL:display:completionHandler: fails


I'm implementing NSApplicationDelegate's application:openFiles in order to get some custom behavior for specific documents. For some documents though, I want the default behavior (which is just opening and displaying them). So after sorting the documents I call for each document:

[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL URLWithString:camDoc] display:YES completionHandler:^(NSDocument *document, BOOL alreadyOpen, NSError *error){
    NSLog(@"%@",document);
}];

In the console the following error is logged:

*** setObjectForKey: key cannot be nil

The completion handler is never called. If I comment out the complete openFiles method, then the documents are opened fine, so it seems the NSDocument class is implemented correctly.

I tried turning off Sandboxing temporarily, as the delegate method receives NSStrings not URL's, so I expected that that might have been the problem. That did not help.

What is going wrong here?

Bonus question: How can I get security scoped URL's to be opened by the application (from the Finder)?


Solution

  • Turned out the problem was due to the way the NSURL is created. That should have used the fileURL methods of course.