3

Here is the code, but for some reason I get nil when I read from Watch. Any idea what am I missing?

Writing in iOS Method

@IBAction func writeDefaults(_ sender: Any) {
    UserDefaults(suiteName: "group.testingGroups")!.set(true, forKey: "myKey")
}

Reading from WatchOS

@IBAction func readDefaults() {
    let readKey = UserDefaults(suiteName: "group.testingGroups")!.bool(forKey: "myKey")
    print("Key Value:  \(readKey)")
}

I also tried the following when reading but nothing...

 let readKey = UserDefaults.init(suiteName: "group.testingGroups")!.bool(forKey: "myKey")

Apple Developer Site:

enter image description here

WatchAppExtension:

enter image description here

WatchApp:

enter image description here

iOS Project:

enter image description here

Rob
  • 14,746
  • 28
  • 47
  • 65
fs_tigre
  • 10,650
  • 13
  • 73
  • 146

1 Answers1

4

Communication between watchKit and app using userDefaults has been removed.
It can be done using watchConnectivity framework

Abhra Dasgupta
  • 525
  • 1
  • 3
  • 12
  • Oh, ok, I thought that the watch connectivity frame work was for heavy data and that userdefaults was still active. Is there any notification from Apple where thy state that this was removed? Thanks – fs_tigre Dec 09 '16 at 03:45
  • 1
    Yes it was removed in watchOS 2, as since watchOS 2 apps run on the watch and not the iPhone. – Abhra Dasgupta Dec 09 '16 at 12:24
  • Ok, I will look into it. Thanks a lot. – fs_tigre Dec 09 '16 at 13:18