2

I'd like to know if it's possible (and how!) to load a file saved in my main app's documents sandbox folder from a widget.

My main app (UIKit) saves images to FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]. as a .png. I have a function to load these images from my app but would like to know how to load them in my widget timeline.

Here's my code for loading the images in UIKit:

    var documentsUrl: URL {
    return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}

private func loadImage(fileName: String) -> UIImage? {
    let fileURL = documentsUrl.appendingPathComponent(fileName)
    do {
        let imageData = try Data(contentsOf: fileURL)
        return UIImage(data: imageData)
    } catch {
        print("Error loading image : \(error)")
    }
    return nil
}

I've got app groups working as I'm using UserDefaults between targets fine.

Thanks!

Noah Evans
  • 309
  • 3
  • 12
  • How do you load them in the App? Please show your code. – pawello2222 Oct 01 '20 at 17:12
  • 3
    You need to use the shared File Container specific for your App Group. See this answer: [Share data between main App and Widget in SwiftUI for iOS 14](https://stackoverflow.com/a/63925053/8697793) – pawello2222 Oct 01 '20 at 19:55

0 Answers0