Define the correct UIDocument subclass with the key UIDocumentClass

Hi there,

I'm trying to migrate my app to using the UIDocumentViewController so I can use the new launch experience. My app exports a custom file type and uses UIDocument + UIDocumentBrowserViewController.

I tried creating a UIDocumentViewController and making it the root view of my app but it doesn't recognise my custom document type.


class Document_VC: UIDocumentViewController {
    
    var storyCardsDocument: PlotCardDocument? {
        self.document as? PlotCardDocument
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        configureViewForCurrentDocument()
    }

    override func documentDidOpen() {
        configureViewForCurrentDocument()
    }

    func configureViewForCurrentDocument() {
        guard let document = storyCardsDocument,
              !document.documentState.contains(.closed)
              && isViewLoaded else { return }

        print("Document opened: \(document.fileURL)")
    }
}

The app opens but when I navigate to a document made in a previous version of the app it is greyed out. I also don't see a 'New' button in the launcher view.

To try and see what I was doing wrong, I tested the markdown app sample code. When I make the UIDocumentViewController the root and try to open or create a new markdown document I get the following error:

no document class found. Define the correct UIDocument subclass with the key UIDocumentClass in the info.plist's CFBundleDocumentTypes dictionary.

Answered by Leatherbarrow in 806366022

I managed to get this working. For anyone else who gets stuck on this, the text before the dot should be your product module name found in the build settings. The sample code uses 'Editor' which is kinda confusing in the context of file types/documents since 'Editor' is also a role. The thing that was throwing me was that my app name has a space but evidently the UIDocumentClass value cannot have spaces.

I wish this stuff was documented somewhere. Maybe ModuleName.Value is a common pattern but I don't see it any other plist entries.

Accepted Answer

I managed to get this working. For anyone else who gets stuck on this, the text before the dot should be your product module name found in the build settings. The sample code uses 'Editor' which is kinda confusing in the context of file types/documents since 'Editor' is also a role. The thing that was throwing me was that my app name has a space but evidently the UIDocumentClass value cannot have spaces.

I wish this stuff was documented somewhere. Maybe ModuleName.Value is a common pattern but I don't see it any other plist entries.

Define the correct UIDocument subclass with the key UIDocumentClass
 
 
Q