Search code examples
swiftbrowserdocumentmac-catalystuidocumentbrowserviewcontroller

Using Document Browser View Controller with Mac Catalyst


I have just started experimenting with Catalyst. My app is a document browser based app.

The stock MacOS Finder dialog is indeed launched when the appropriate button is clicked. The main app window completely disappears when the Finder dialog appears, unless I choose in the IB for the document browser view controller to appear in "Automatic" mode.

Cancelling the operation indeed brings back the main window.

However, selecting a file will yield a blank screen and no results. A little debugging revealed that none of the file selection functions is being called, and I have implemented all of them:

func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {...}

func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {...}

func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {...}

Is there another function or handle used in Catalyst? I found nothing in the documentation.

EDIT: I should clarify that I manipulated the app to present the DocumentViewController before the DocumentBrowserViewController, although Apple requires that the DocumentBrowserViewController is the initial view controller. I did it by changing the app delegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

...

// Set the documentViewController to appear first
    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let initialViewController = storyboard.instantiateViewController(withIdentifier: "main")

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

    return true
}

Taking this out still doesn't change anything. And a default project created from the document browser template does seem to work. What could prevent these methods from being called?


Solution

  • I would suggest to implement func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentsAt documentURLs: [URL]) as well