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!