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.

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

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.

[retracted]

I also found that using UserDefaults.standard did not work. What helped was creating an instance of UserDefaults in both my Watch app and its complication. These two definitions use the group name from the Apps Group setting.

Each have a UserSettings.swift file (name not important) where I keep data that needs to be shared.

class UserSettings: ObservableObject {
    // Define the UserDefaults instance that gets shared. 
    // If it can't then at least return UserDefaults.standard. 
    // Lets the code work when you're not focusing on complications yet.
   let userDefaults: UserDefaults = {
        if let defaults = UserDefaults(suiteName: "group.MyGroupName") {
            return defaults
        }
        return UserDefaults.standard
    }()

    var someData: Int {
        didSet {
           userDefaults.set(someData, forKey: "someDatakey")
        }
    }
}

FYI, you can also add @Published to the individual variable definitions (e.g., someData) if your app needs to respond to changes.

(I'm also trying to figure out how to have the complication automatically update when the app changes someData.)

Userdefaults for complications are nil while watch is working fine
 
 
Q