0

I have a flutter app that stores a file in the iOS applications directory for the home screen widget to display. The file path is shared using UserDefaults. The following is some sample code used:

let data = UserDefaults.init(suiteName: widgetGroupId)
        let filePath = data?.string(forKey: "_filepath")
        var displayMessage = "Default display message"
        
        // if file path found
        if (filePath != nil) {
            let fileManager = FileManager.default
            
            // update display message if file at filePath does not exist
            if (!fileManager.fileExists(atPath: filePath!)) {
                displayMessage = "Image couldn't be displayed :("
                displayMessage = filePath! // TODO: remove this
            }
        } else {
            displayMessage = "No image set yet!"
        }

This works in the simulator and fileExists returns true. Upon testing on a real device, it always returns false. An example file path used on a real device is:

var/mobile/Containers/Data/Application/<UUID>/Documents/file.jpg

How do I resolve this?

Emmanuel
  • 315
  • 1
  • 3
  • 15

1 Answers1

0

You need to use AppGroup to share files between main app and widget. Here is the link to another thread that describes how to use AppGroups.

  • You should include details in the answer itself along with any links, as links can become outdated. – Paulw11 Apr 22 '23 at 02:07