I am working on a document based app in swift using UIDocumentBrowserViewController. I would like the app to default to opening the previous file that was used in the last session. When exiting the app I save the file URL in UserDefaults. When I restart the app, UIDocumentBrowserViewController presents its view. I would like to add in viewDidLoad() an if-statement that checks if a fileURL exits in UserDefaults. If it does I would like to select that file, open it and present it. The simplest approach would seem to be to figure out how to piggyback off of the existing presentDocument(at: documentURL) methods I am using with UIDocumentBrowserViewController, or perhaps off of one of the UIDocumentBrowserViewControllerDelegate methods; In the other words I would like to programmatically select a file in UIDocumentBrowserViewController and present it. However, when I try this the document fails to open, in the UIDocument method open(completionHandler: ) (i.e. the completion handler reports a failure). I am sure I have the correct fileURL, also the file I am attempting to do this with is in the cloud directory Any Ideas on how this should be implemented?
open document programmatically from UserDefaults in UIDocumentBrowserViewController
285 views Asked by siege097 At
1
I figured out you have to use a bookmark instead of a URL. The code to create a bookmark and save it to
UserDefaultsis as follows:and the code to retrieve the bookmark, convert it to a URL, and present the document is as follows: