Passing Data from Watch to iPhone using Appgroups

224 Views Asked by At

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!")
    }
}
1

There are 1 best solutions below

0
Dávid Pásztor On

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.