Userdefaults for complications are nil while watch is working fine

I'm able to successfully send userdefaults from the phone to the watch using applicationContext and save those userdefaults to the app group I created. For the watch, I see all the userdefaults come in successfully and I'm able to use them in the WatchOS app.

Once I created the complications for the watch app, I am using the same app group ID and I added that app group in the capabilities of the watch extension but when I try to use the same userdefault that I'm using for the watch, everything is nil. I'm not understanding what I'm doing wrong. How do I share userdefaults between the watch and complication?

This is how I'm bringing in the userdefaults from the phone to the watch

        
        let defaults = UserDefaults(suiteName: K.appGroupID)
        
        if let value1 = applicationContext["lat"] as? Double {
            defaults?.setValue(value1, forKey: "lat")
        }
        if let value2 = applicationContext["lng"] as? Double {
            defaults?.setValue(value2, forKey: "lng")
        }
         ...

I am able to use this exact function in the watch app, but in the complication, the userdefault is nil.

    let formatter = DateFormatter()
    formatter.timeStyle = .short
    formatter.timeZone = .current
    
    let defaultLoad = UserDefaults.init(suiteName: K.appGroupID) 
    if defaultLoad!.bool(forKey: "timeFormat") == true {
     ...

I tried to remove and re-add the app group but that didn't work either. I also created a separate app group for the watch and extension to use alone but still same issue.

Post not yet marked as solved Up vote post of GrayFeather Down vote post of GrayFeather
1.2k views

Replies

Hey there!

Have had a similar issue after migrating ClockKit complications to WidgetKit. 🤓 You must enable App Group not only to Watch Widget Extension, but for the Watch app target as well. This should solve your issue (solved for me).

P.S. I still have issues with WidgetKit complications not updating, even though Watch Widget Extension has access to the data: https://developer.apple.com/forums/thread/735352

  • @Serzhas Yes you’re correct I ended up doing that and it finally worked. Also I have the same issue as you. Even when I run WidgetCenter.shared.reloadAllTimelines() it does not always refresh the widgets even though in iOS this works. Only way to refresh is to go back and forth between watch faces.

Add a Comment

Did not work for me, FYI. I have the same App Group shared for all my targets, and can post data from the watch app but cannot see it in the WidgetKit complication.

  • Same. I use same App Group for both targets. The app on watch works fine, but the widget cannot see updated userDefaults.

Add a Comment

[retracted]