Search code examples
iosswiftuidocumentbrowserviewcontroller

Make UIDocumentBrowserViewController immediately create new document on first launch


I have a Document Based app working well with UIDocumentBrowserViewController. However, for brand new users who have no documents it's a really confusing first experience to just be launched directly in to a document browser. I have made it detect if there are any local documents in my file format and show an onboarding screen if not, but I would also like it to just "skip" the document browser altogether and immediately create a new document the first time.

However, the whole lifecycle of everything seems so tied in to the document browser, not to mention all the clever stuff it does around importing files in to iCloud etc. It doesn't seem I can just bypass the document browser. What is the best way to handle this?

I tried to just create a new file on launch and use revealDocument(at:importIfNeeded:completion:) but I got a EXC_BAD_ACCESS crash (without any error message) so not sure if that's the right avenue to explore.


Solution

  • I ended up creating a new file, presenting the document browser and then in the completion: handler calling revealDocument(at: url, importIfNeeded:true, completion:) with a completion handler than calls browser.presentDocument(at: documentURL)