0

to make it short, my iOS app download some data from a server and put it into an array. I wanna share the array count with the widget using the AppGroups. I save the array count number to UserDefaults like this:

    if let userDefaults = UserDefaults(suiteName: "group.com.etc") {
           // I save just a simple Int
           userDefaults.set(loaded.count, forKey: userDefaultsKey)
       }

Then on the widget side I have this class to retrieve the data:

class MyDataProvider {
    
    static func getCountFromUserDefaults()-> Int {
        
        if let userDefaults = UserDefaults(suiteName: "group.com.etc") {
            let myFlag = userDefaults.integer(forKey: userDefaultsKey)
            print("myFlag is \(myFlag)")
            return myFlag
        }
        print("my flag is 0")
        return 0
    }
    
}

Last, my getTimeLine func is this

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []

        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .second, value: hourOffset * 30, to: currentDate)!
            let entry = SimpleEntry(date: entryDate, myString: "\(MyDataProvider.getCountFromUserDefaults())")
            print("my entry is \(entry)")
            entries.append(entry)
        }

        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }

The issue is that the number is always 0. On iOS side I'm sure the number is saved correctly but the widget get always 0 even when the iOS app is opened.

Do i mistake something?

I Don't Tell
  • 264
  • 1
  • 15

1 Answers1

0

Solved: I forgot to add the AppGroup to the Widget Extension too.

I Don't Tell
  • 264
  • 1
  • 15