24

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
K.A.Q
  • 469
  • 4
  • 13
  • 1
    Have you had any success? I have the same scenario. – Chad Milburn Sep 15 '20 at 20:59
  • Unfortunately, no. I thought that maybe Apple would do something about it in their final release of iOS 14 and WidgetKit, but, they have not. If you can up vote the question, please do, so it can get more attention. – K.A.Q Sep 16 '20 at 22:05
  • I am facing the same problem, I could not find a solution :( – FarouK Oct 30 '20 at 17:44
  • Same issue here, I could not find any solution about it. – Alihan Oct 30 '20 at 20:20
  • Same problem. PS: Method WidgetCenter.shared.reloadAllTimelines() - reloads all your widgets. Should be called from your main app when the user has changed data related to a widget. – Yanis Nov 14 '20 at 12:31
  • I have the same problem. There is still no solution unfortunately... – A. Poltoratskyi Dec 04 '20 at 17:01
  • 2
    I'm having the same problem. One possibility I'm thinking about could be to ignore the selected configuration options within `IntentTimelineProvider` if we can verify they're stale, and show the default instead. – diegoreymendez Dec 04 '20 at 19:54
  • related this, how can we disable getting options from user when user logs out? instead of option selection, can we show some string which tells user to login. – Nandha Dec 06 '20 at 01:52
  • Any Progress on this one? Same issue with no solution over here. – Sebastian Boldt Jan 09 '21 at 21:02
  • 1
    Unfortunately there is no solution to this so far. This is very bad design on Apple's part. – K.A.Q Jul 09 '21 at 13:05

3 Answers3

3

You can't. The only option you have is to ignore the configuration option on the widget side and show some kind of error message to the user that their config needs to be updated. INExtension does not provide a way to handle this (yet).

halfer
  • 19,824
  • 17
  • 99
  • 186
cup_of
  • 6,397
  • 9
  • 47
  • 94
0

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)
   }

}
Yanis
  • 167
  • 1
  • 6
  • 1
    It doesn't look like this is called when `WidgetCenter.shared.reloadAllTimelines()` is called, it only seems to be called when you edit a widget. I guess the most interesting concept in this code fragment is using `UserDefaults` initialized to an app group or other means of "communication" (like files in an app group container). – DarkDust Sep 15 '22 at 15:18
-2

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.

freshking
  • 1,824
  • 18
  • 31