2

I have an action extension and its accompanying app. In my action extension, files are created, and I want to be able to access them from my main app.

Using UserDefaults is not an option because I have to share relatively big files (including images, videos, etc.).

How can I achieve this?

Not helpful. Also not helpful.

Community
  • 1
  • 1
IHaveAQuestion
  • 789
  • 8
  • 26
  • Use a Shared Container to share data: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html – David S. Mar 08 '17 at 22:18
  • @DavidShaw, it says there, though, that user defaults can be shared; there is no mention or implication of sharing files as far as I can tell. – IHaveAQuestion Mar 08 '17 at 22:24

1 Answers1

4

The way you do this is with an App Group. As you saw, you can share using UserDefaults, but you can also share using the FileManager.

  1. Create App Group under Targets->Capabilities:

App Group Enable

  1. Access the directory via FileManager:

    fileprivate func sharedContainerURL() -> URL? {
        let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.dsdevelop.appgroup")
        return groupURL
    }
    
    func writeSharedData(data:Data, to fileNamed:String) -> Bool {
    
        guard let url = sharedContainerURL() else {
            return false
        }
    
        let filePath = url.appendingPathComponent(fileNamed)
    
        do {
            try data.write(to: filePath)
            return true
        } catch {
            print("Write failed: \(error)")
            return false
        }
    } 
    
Tiago Mendes
  • 4,572
  • 1
  • 29
  • 35
David S.
  • 6,567
  • 1
  • 25
  • 45