0

I am working on a project to pass data from an Apple Watch App to the iPhone App using Appgroups. My code is not working and I am not sure why. Hopefully someone can help me out! :)

Sending Data Apple Watch

@IBAction func senddata() {
    let group = "group.pairedapp"
    let shared = UserDefaults(suiteName: group)
    let ok = "works"
    shared!.setValue(ok, forKey: "status")

    shared!.synchronize()    
}

Getting Data on iPhone

@IBAction func getWatchData(_ sender: Any) {    
    let group = "group.pairedapp"
    let shared = UserDefaults(suiteName: group)

    let get = shared!.value(forKey: "status")
    if get != nil {
        print("works")
    }
    else{
        print("OO NO!")
    }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • Don't do that. Use [`WCSession`](https://developer.apple.com/documentation/watchconnectivity/wcsession) – Paulw11 Nov 27 '17 at 01:32
  • 1
    Use WatchConnectivity for data sharing. https://stackoverflow.com/questions/34365131/how-to-share-data-using-watch-connectivity-when-working-with-core-data/34410018#34410018 – Muneeba Nov 27 '17 at 07:27

1 Answers1

0

Since the introduction of watchOS2, watchOS apps are no longer considered just extensions of their iOS counterpart and hence you cannot use AppGroups to share data between the two.

You should use the WatchConnectivity framework on watchOS2+ to share data between your watchOS and iOS apps.

For more information, see the Sharing Data part of the WatchKit Programming Guide.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116