How to update list of offered watchOS complications dynamically

165 Views Asked by At

In my watch app the AppIntentRecommendation that is returned by func recommendations() needs to be updated after the app is launched.

Problem: The information that I need to return a list of AppIntentRecommendation is not available when the app is first installed, so I return a "dummy" AppIntentRecommendation initially. After the app is launched for the first time and user has signed in, I can update the AppIntentRecommendation in func recommendations() but watchOS does not update the list of widgets presented as complications.

    func recommendations() -> [AppIntentRecommendation<ConfigurationAppIntent>] {
        // Create an array with all the preconfigured widgets to show.
        let defaults = UserDefaults.init(suiteName: "group.myApp")!
        guard let names = defaults.dictionary(forKey: "names" as? [String:String] else {
            return [AppIntentRecommendation(intent: .Demo, description: "Demo")]
        }

        var recs = [AppIntentRecommendation<ConfigurationAppIntent>]()
        for (idx, name) in names() {
            let rec = ConfigurationAppIntent()
            rec.order = idx
            rec.carName = name
            rec.action = nil
            recs.append(AppIntentRecommendation(intent: rec, description: "name") )
        }
        return recs
    }
}
1

There are 1 best solutions below

2
RawMean On

The solution is easy but it's deep in the documentation:

WidgetCenter.shared.invalidateConfigurationRecommendations()

This will reload the recommendations