react-native-shared-group-preferences icon indicating copy to clipboard operation
react-native-shared-group-preferences copied to clipboard

Not shared data between react native and widget

Open Youthleap423 opened this issue 4 years ago • 19 comments

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?

Youthleap423 avatar Aug 27 '21 12:08 Youthleap423

my log is: [User Defaults] Couldn't read values in CFPrefsPlistSource

Youthleap423 avatar Aug 27 '21 13:08 Youthleap423

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

csumrell avatar Dec 30 '21 19:12 csumrell

+1

duyhodev avatar Apr 25 '22 15:04 duyhodev

+1

dominickmalzone avatar Apr 25 '22 18:04 dominickmalzone

+1 extension also

panutat avatar Apr 29 '22 07:04 panutat

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

duyhodev avatar May 25 '22 12:05 duyhodev

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.

Screenshot 2023-07-07 at 9 45 40 PM

quant-daddy avatar Jul 08 '23 01:07 quant-daddy