pawello2222 / WidgetExamples

A demo project showing different types of Widgets created with SwiftUI and WidgetKit.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data sharing from iOS app to Lock Screen widget and Watch widget

BigabilityLiu opened this issue · comments

Hi bro, thanks for these examples.

I'm working on iOS 16 / watchOS 9 Widget these days.
Before, I have a Watch Complications (ClockKit), it gets data from iOS app every 10 mins through WatchConnectivity, it works well.
But on watch 9, it needed to migrate to WidgetKit (Migrating ClockKit complications to WidgetKit), then WatchConnectivity doesn't work anymore, shows error "Companion app is not installed".
I have also tried share data on App Group UserDefault between iOS app and Watch Widget, but when read data on widget it shows error "[User Defaults] Couldn't read values in CFPrefsPlistSource<0x...> (Domain: group.com...., User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd"

I'm trying to use intent to configure/update widget data (Making a Configurable Widget), but I'm not sure if it can update my Widget every 10 mins, or Intents is not for this use at all.

If not, the only way is use URLSession to request data from http server.

So do you have any suggestions base on your experience,

Thank you from your time

Hi,

Refreshing every 10 mins is rather not possible using WidgetKit.

From the documentation:

To manage system load, WidgetKit uses a budget to distribute widget reloads over the course of the day. The budget allocation is dynamic and takes many factors into account, including:

  • The frequency and times the widget is visible to the user.
  • The widget’s last reload time.
  • Whether the widget’s containing app is active.

WidgetKit maintains different budgets for each active widget the user adds to their device. For example, if the user adds two instances of a configurable sports widget, showing information for two different teams, each widget has its own budget.

A widget’s budget applies to a 24-hour period. WidgetKit tunes the 24-hour window to the user’s daily usage pattern, which means the daily budget doesn’t necessarily reset at exactly midnight. For a widget the user frequently views, a daily budget typically includes from 40 to 70 refreshes. This rate roughly translates to widget reloads every 15 to 60 minutes, but it’s common for these intervals to vary due to the many factors involved.

A daily budget containing from 40 to 70 refreshes means you can probably update a widget around 1-3 times per hour (I'd assume not every widget can refresh itself every 15 minutes as mentioned above).

You can find more information here:

Thank you for your kindly response,

Actually the main thing I want to discuss is the way of data sharing between iOS App and Widget.

I'm going to close this issue for now. I'll be back if i find a better way to share data further.

Please see the AppGroupWidget as an example of how to share data between the App and the Widget (using UserDefaults of FileManager).