eternalstorms
eternalstorms

Reputation: 194

iOS: Support App Group folders in local file provider

I understand that if I provide the UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace keys in Info.plist for my iOS app, files in my app's Documents folder are shown in Files.app ("On This iPad") and the documents browser.

Now my question - aside from writing a File Provider extension - is there any way to add an app group's folder or replace the Documents folder with a folder in the app group's folder? For sharing between my extensions, I save everything in the App Group folder instead of Documents, but then, of course, Files.app doesn't have access to those files, that's why I'm asking.

I tried creating symbolic links from the documents folder to the actual file, and they are shown, but don't work correctly ("file doesn't exist").

I've also created a File Provider extension, but it was rejected by Apple because my extension isn't cloud-backed and was only for local files. According to my reviewer, a File Provider extension must be cloud-storage backed.

Thank you for your help and insights,

-Matt

Upvotes: 9

Views: 2095

Answers (2)

olynoise
olynoise

Reputation: 2056

In the main app, bookmark the documents URL and save to the App Group's user defaults. Then access the bookmark from inside the App Extension. This requires that the main app has been opened once and runs this code before the app extension runs. (Thanks to BeepStreet developer for providing me with this answer!)

Psuedo code follows... Main App

 let documentsDirectory = try FileManager.default.url(for: .documentDirectory,
                                                         in: .userDomainMask,
                                                         appropriateFor: nil,
                                                         create: true).path


   let appGroup: String = "group.app.example.etc"
   let bookmark = documentsDirectory.bookmarkData(options: .minimalBookmark,
                                                                 includingResourceValuesForKeys: nil,
                                                                 relativeTo: nil)
            
   if let userDefaults = UserDefaults(suiteName: appGroup) {
                userDefaults.set(bookmark, forKey: "documentsDirectory")
   }

App Extension

 let userDefaults = UserDefaults(suiteName: appGroup),
        let bookmark = userDefaults.value(forKey: "documentsDirectory") as? Data
    {
            let resolvedURL = try URL(resolvingBookmarkData: bookmark, options: [], relativeTo: nil, bookmarkDataIsStale: nil)

     }

don't forget to access the bookmark appropriately....

resolvedURL.startAccessingSecurityScopedResource()

Upvotes: 0

Michael Ochs
Michael Ochs

Reputation: 2870

It's pretty simple: No. :(

We are struggling with the same issue and its simple: You can't participate in the files app (or iTunes File Sharing) if you don't store your files in the Documents folder.

It seems like this is not well thought out. You should not integrate as a file provider unless you are actually providing a file system such as dropbox, at least that's what we've been told at WWDC. On the other side you should support stuff like iMessage extensions, today extensions,... which only are possible if your files are accessible through an app group. But this then automatically breaks the use of the Files.app as well as iTunes Filesharing.

We filed radars for that, any duplicate would help, I guess.

Upvotes: 3

Related Questions