How to reset intent extension configurations in WidgetKit

旧街凉风 提交于 2021-02-06 10:18:08

问题


I have an application where a user can login. If a user is logged in, then I display a placeholder for my widget and it's configurable through an intent extension.

The configuration has two options:

  • the first option depends on the username of the user currently logged in.
  • the second option depends on the value of the first option.

This works fine for 1 user, however, if the user logs out, then logs in with a different account, the old selected options are still selected when they try to configure the widget, which are wrong options since it's now a different user. Also, the old data is still shown in the widget.

How do I reset the configuration of a widget in WidgetKit? Meaning, when the user tries to configure it all the previous configurations would be unselected and empty.

I tried

WidgetCenter.shared.reloadAllTimelines()

But that only reloads the timeline methods but does not reset configurations.

I've looked at Apple's documentations and could not find anything about it either.

Thanks a lot,


回答1:


I had the same issue that the widget was showing stale configuration options. I solved this by forcefully refreshing all objects in my shared Core Data database which I was using. I did this in the „getTimeline“ method.




回答2:


I found this solution, but for dynamic properties only: use different identifiers for every user in your IntentHandler. Example:

class IntentHandler: XXExtension { ... }

extension IntentHandler: XXDynamicXXXSelectionIntentHandling {

   func provideXXXOptionsCollection(for intent: XXDynamicXXXSelectionIntent, with completion: @escaping (XXObjectCollection<XXX>?, Error?) -> Void) {
      let userId = (UserDefaults.myGroup().object(forKey: "UserId" ) as? String) ?? ""
      let items: [XXX] = UserDefaults.myGroup().sharedXxx.map { (sharedXxx) -> XXX in
         return XXX(identifier: userId + "_" + sharedXxx.Id // <== add unique prefix ===
                       display: sharedXxx.visibleName())
      }        
      let collection = XXObjectCollection(items: items)
      completion(collection, nil)
   }

}


来源:https://stackoverflow.com/questions/63764083/how-to-reset-intent-extension-configurations-in-widgetkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!