<!--
{
  "availability" : [
    "macOS: 10.7.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocumentController/openDocument(withContentsOf:display:completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocumentController(im)openDocumentWithContentsOfURL:display:completionHandler:"
  },
  "title" : "openDocument(withContentsOf:display:completionHandler:)"
}
-->

# openDocument(withContentsOf:display:completionHandler:)

Opens a document located by a URL, optionally presents its user interface, and calls the passed-in completion handler.

```
func openDocument(withContentsOf url: URL, display displayDocument: Bool, completionHandler: @escaping (NSDocument?, Bool, (any Error)?) -> Void)
```

## Parameters

`url`

The URL locating the document to open.

`displayDocument`

If <doc://com.apple.documentation/documentation/Swift/true>, displays the document’s user interface.

`completionHandler`

The completion handler block object passed in to be called at some point in the future, perhaps after the method invocation has returned. The completion handler must be called on the main thread.

    The block takes three arguments:

- `document`: The document that was opened, if successful. Otherwise, `nil`.
- `documentWasAlreadyOpen`: Whether the document was already open or being opened when this method was called.
- `error`: If not successful, an `NSError` object that encapsulates the reason why the document could not be opened.

## Discussion

The default implementation of this method checks to see if the document is already open or being opened, and if it is not determines the type of the document, calls [`makeDocument(withContentsOf:ofType:)`](/documentation/AppKit/NSDocumentController/makeDocument(withContentsOf:ofType:)) to instantiate it, and calls [`addDocument(_:)`](/documentation/AppKit/NSDocumentController/addDocument(_:)) to record its opening. If `displayDocument` is <doc://com.apple.documentation/documentation/Swift/true> and the document is not already open, the default implementation calls [`makeWindowControllers()`](/documentation/AppKit/NSDocument/makeWindowControllers()) and [`showWindows()`](/documentation/AppKit/NSDocument/showWindows()). If the document is already open, the implementation just calls [`showWindows()`](/documentation/AppKit/NSDocument/showWindows()) if `displayDocument` is <doc://com.apple.documentation/documentation/Swift/true>. If the relevant document class returns <doc://com.apple.documentation/documentation/Swift/true> when sent [`canConcurrentlyReadDocuments(ofType:)`](/documentation/AppKit/NSDocument/canConcurrentlyReadDocuments(ofType:)) then the invocation of [`makeDocument(withContentsOf:ofType:)`](/documentation/AppKit/NSDocumentController/makeDocument(withContentsOf:ofType:)) is done on a thread other than the main one, and when that has returned, the rest of the operation is done on the main thread.

The default implementation of this method uses the file coordination mechanism that was added to the Foundation framework in OS X v10.7. All of the work it does is one big coordinated read, and it passes the document to the `NSFileCoordinator` method <doc://com.apple.documentation/documentation/Foundation/NSFileCoordinator/addFilePresenter(_:)> right after calling [`addDocument(_:)`](/documentation/AppKit/NSDocumentController/addDocument(_:)). (The balancing invocation of the `NSFileCoordinator` method <doc://com.apple.documentation/documentation/Foundation/NSFileCoordinator/removeFilePresenter(_:)> is in the `NSDocument` method [`close()`](/documentation/AppKit/NSDocument/close()).)

You can override this method to customize how documents are opened. Its implementation, however, is somewhat complex, so you should generally investigate overriding one of the methods that it calls instead. However, you can override this method to do additional work before calling the underlying method on `super`. You can also call the underlying method on `super` with a custom completion handler that performs additional work before calling the original completion handler. If you do override this method you should investigate whether you should also override [`reopenDocument(for:withContentsOf:display:completionHandler:)`](/documentation/AppKit/NSDocumentController/reopenDocument(for:withContentsOf:display:completionHandler:)) to apply the same customization. In either case, take care to always call the completion handler on the main thread.

You can call this method to open a document.

### Special Considerations

For backward binary compatibility with OS X v10.6 and earlier, the default implementation of this method calls `[self openDocumentWithContentsOfURL:url display:displayDocument error:&anError]` if that method or the even older [`openDocumentWithContentsOfFile:display:`](/documentation/AppKit/NSDocumentController/openDocumentWithContentsOfFile:display:) method is overridden and this one is not, instead of calling [`makeDocument(withContentsOf:ofType:)`](/documentation/AppKit/NSDocumentController/makeDocument(withContentsOf:ofType:)) and all the rest.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)