iOS: Support App Group folders in local file provider
Asked Answered
R

2

9

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

Ripen answered 22/9, 2017 at 10:16 Comment(0)
P
3

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.

Propinquity answered 25/9, 2017 at 10:10 Comment(3)
You can place files into an app group, but have to add a FileProvider extension - then you can see your files in Files.app. It's true the documents would not be visible in iTunes though.Smalltime
Has your radar ever received a reaction? I just posted a similar question, and discovered your answer only after StackOverflow marked it as related. Did you find a solution in the meantime?Michelinemichell
my File Provider was eventually allowed on the App Store, so that's the route I took.Ripen
D
0

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()
Dantzler answered 3/1 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.