Store data separately for each instance of a widget

Is there any way for a widget to store data in a way that is separate from other instances of the same widget? If I save data to UserDefaults.standard, other widgets have access to the same data.

I could force the user to configure the app manually, but I would rather prefer to give the widget a set of default parameters when it is added, based on the state of the parent app. These parameters must be saved somewhere, possibly in UserDefaults using a key unique to the widget instance.

There seems to be an ID which is part of the WidgetInfo structure, but I can't find out how to read this.

When saving to user defaults, you can set different values by using different keys. For example.

UserDefaults.current.set(value: true, forKey:”myKey”)

And you use said key to retrieve the value. Using different keys for different values will give you the results you are looking for.

Store data separately for each instance of a widget
 
 
Q