UIDocumentBrowserViewController.revealDocument() not working as expected?

Hi,


I've an app adopted the iOS 11 UIDocumentBrowserViewController. I've declared my own document type (UTI) and the document creation process worked well.


However, when it's about to "import" a file, I called UIDocumentBrowserViewControlller.revealDocument() by passing the URL I got from application(open: options: ). The completion showed no errors, but I cannot see the document been imported as expected.


The "revealedDocumentURL" is actually pointing to an "Inbox" folder under the app container's Documents folder. Something like:

file:///<App Container>/Dcouments/Inbox/filename.extension

But nothing inside that folder (nor the folder itself) is visible in the DocumentBrowser, nor in the Files app.


Further more, while revealDocument (with importIfNeeded=true) being called, the delegate's documentBrowser(_:didImportDocumentAt:toDestinationURL:) function is never called. But according to the documentation here , it should call the delegate function before calling completion handler.


Can anyone please advice what's the correct way to handle file import with the DocumentBrowserViewController?


Thanks a lot,

-Robin

Can you show how you define the url ?


I usually get the documents directory with as follows:


    class func dataFilePath() -> String {
      
        let paths = NSSearchPathForDirectoriesInDomains(
            FileManager.SearchPathDirectory.documentDirectory,
            FileManager.SearchPathDomainMask.userDomainMask, true)
        let documentsDirectory = paths[0] as NSString 
        return documentsDirectory.appendingPathComponent("data.archive") as String
    }


May see also:

h ttps://developer.apple.com/documentation/uikit/uidocumentbrowserviewcontroller

The scenario is when my app receives a file from other sources (like receiving file from AirDrop, or opening an attachment in Mail), so it goes to:

func application(_ app: UIApplication, open inputURL: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
       // Ensure the URL is a file URL
       guard inputURL.isFileURL else { return false }
               
       // Reveal / import the document at the URL
       guard let documentBrowserViewController = window?.rootViewController as? DocumentBrowserViewController else { return false }

       documentBrowserViewController.revealDocument(at: inputURL, importIfNeeded: true) { (revealedDocumentURL, error) in
            if let error = error {
                // Handle the error appropriately
                print("Failed to reveal the document at URL \(inputURL) with error: '\(error)'")
                return
            }
           
            // Present the Document View Controller for the revealed URL
            documentBrowserViewController.presentDocument(at: revealedDocumentURL!)
        }

        return true
}


(Actually this is part of the Xcode's Document Based App template. And I think I don't have to make my own URL there.)

The question is that, calling revealDocument() does not make the input file visible in the DocumentBrowserViewController.

I am experiencing the same issue. Have you resolved it?

UIDocumentBrowserViewController.revealDocument() not working as expected?
 
 
Q