react-native-shared-group-preferences
react-native-shared-group-preferences copied to clipboard
Not shared data between react native and widget
I have made widget extension with app and tried to share data between them. but It is not working. I can't get data on widget. Can you tell me about issues?
my log is: [User Defaults] Couldn't read values in CFPrefsPlistSource
same thing for me, following the instruction on the readme this, I can not get darta on widget. It shows it saves in the app but it does not show up in the widget or userdefault storage
+1
+1
+1 extension also
Finally I managed to get it work
-
The identifier for app group should have some postfix, for example, your main app identifier is com.domain.myapp, then you app group identifier should be group.com.domain.myapp.store
-
In widget side, we can get the data like so in the timeline `
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) { let userDefaults = UserDefaults(suiteName:"group.com.domain.myapp.store) var user: User? do { let shared = userDefaults?.string(forKey: "user") print("data", shared) if(shared != nil){ user = try JSONDecoder().decode(User.self, from: shared!.data(using: .utf8)!) } } catch{ print(error) } let entry = UserEntry(date: Date(), configuration: ConfigurationIntent(), user: user) let timeline = Timeline(entries: [entry], policy: .atEnd) completion(timeline) }
` then in the entry view, you can get this user data by entry.user
- Can also use react-native-widget-bridge, call
WidgetBridge.reloadWidget('MyApp_Widget')every time you do some changes
I'm not sure this is the best way to deal with it or not, but it works for me
I think the group identifier can be anything. Make sure to add same App Group and check the check box in both the app and the widget configuration.