Access file from File app works inside my application, but doesn't work if I launch my app from the File app

Hi!

I've got an application that can handle json (export/import). To achieve that I created a new Document Type in the info.plist, filed the imported and exported Type Identifiers, and created a new UTType.

At first I was trying on the simulator and everything worked fine. I was able to created files and store them in the File app, and then read them back. I also implemented the func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) to handle documents opened from outside the application.

Now when I moved on onto a real device, I noticed that I had to test the access with

guard url.startAccessingSecurityScopedResource() else {
   throw ImportError.accessDenied
}
defer { url.stopAccessingSecurityScopedResource() }

but I get a weird behavior : when opening a file from File app with my app launched, everything works fine. But if I try to launch my application (previously killed) from a file in the File app, startAccessingSecurityScopedResource always returns false

I tried to add the NSFileCoordinator().coordinate(readingItemAt: url, error: &error) { (url) in but I get the same behavior.

I guess my files and UTType are correctly declared as I only see them from inside the `UIDocumentPickerViewController``

and from the File app, I see my application in the share sheet

What am I missing here ? Thanks

Accepted Reply

Ok, a few things here... First of all, to be able to open directly files from the File app, I had to add to the info.plist UISupportsDocumentBrowser YES. When running from the simulator, all files were passed to my application though

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { ... }

but as soon as I added the UISupportsDocumentBrowser YES. now I fell back to the regular way of launching an app from a file which is

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        ...
        // a file was passed through its url
        if let url = connectionOptions.urlContexts.first?.url {
            // do whatever you need to do with this file, but use startAccessingSecurityScopedResource()
        }
    }

Now it works. I hope it will help others :-)

  • Thanks for sharing!

Add a Comment

Replies

Ok, a few things here... First of all, to be able to open directly files from the File app, I had to add to the info.plist UISupportsDocumentBrowser YES. When running from the simulator, all files were passed to my application though

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { ... }

but as soon as I added the UISupportsDocumentBrowser YES. now I fell back to the regular way of launching an app from a file which is

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        ...
        // a file was passed through its url
        if let url = connectionOptions.urlContexts.first?.url {
            // do whatever you need to do with this file, but use startAccessingSecurityScopedResource()
        }
    }

Now it works. I hope it will help others :-)

  • Thanks for sharing!

Add a Comment