0

Sharing UserDefault between targets doesn't work. I have set App Group for both targets but the data is not available on watchOS. See my simplified code:

On iOS:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let defaults = UserDefaults(suiteName: "group.my-bundle-id")!
    defaults.set(true, forKey: "testKey")

    return true
}

On watchOS:

func applicationDidFinishLaunching() {
    let defaults = UserDefaults(suiteName: "group.my-bundle-id")!
    let uddata = defaults.bool(forKey: "testKey")
    print(uddata) // Still returns false
}

This is the app group (which is set for EACH target!):

enter image description here

SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96

1 Answers1

1

According to this:

Modern versions of watchOS run your WatchKit extension on the Apple Watch. As such, you can’t share content between your iOS app and your WatchKit extension via an App Group; you’ll have to use some sort of networking technology. Many folks use WatchConnectivity for this.

// I've looked through tons of tutorials online …

It sounds like those tutorials were written for an older version of watchOS, where the WatchKit Extension ran on the iPhone. This hasn’t been the case since watchOS 2.

SwiftiSwift
  • 7,528
  • 9
  • 56
  • 96