Passing Data to Apple Watch App

Passing data to Apple Watch app

This applies to OS 1 only. See below for better answers.

I got it working using your method. I guess there's a couple of things you can check:

1) Are you synchronising the defaults after you set the value:

defaults?.synchronize();
NSLog("%@ ", defaults?.dictionaryRepresentation())

2) Have you enabled the App Group in both your app and your extension?

App Group capability for App Target
App Group capability for Watch Extension Target

3) Are you using the correctly named app group when constructing the NSDefaults? For example, I use:

NSUserDefaults(suiteName: "group.com.brindysoft.MyWatch");

Once all that's set up I run the app, set the value in the defaults, then run the glance target which reads the value from the default and that seems to work!

iOS Sample Image 49


  1. Still stuck? check your app groups in your apple account

Is there an official way to pass data between Apple Watch and iPhone?

It is possible.

Looking at: The WatchKit Doc's

There is a paragraph on sharing data between the watch app and the extension on the iPhone.

To quote the first paragraph.

Sharing Data with Your Containing iOS App

If your iOS app and WatchKit extension rely on the same data, use a shared app group to store that data. An app group is a secure container that multiple processes can access. Because your WatchKit extension and iOS app run in separate sandbox environments, they normally do not share files or communicate directly with one another. An app group lets the two processes share files or user defaults information between them.

From what I understand MMWormhole is handy for as close to realtime data changes between the 2 binaries. Whereas this method allows for accessing data used saved by the iPhone app that can be read by the Watch App and Vice Versa.

Apple Watch Not Passing Data to iPhone - Swift

I think you have messed up in the sendMessage(), I cannot work out the replyHandler syntax, and you miss the errorHandler: parameter.

Anyway, I've tried your code, and with a few changes it would work.

1). In InterfaceController, the sendPressed():

    var count = 0    
@IBAction func SendPressed() {
//Send Data to iOS
let msg = ["Count" : "\(count)"]

if session.isReachable {
session.sendMessage(msg, replyHandler: nil, errorHandler: { (error) -> Void in
print("Error handler: \(error)")
})
count += 1
}
}

I've added a count, since the message must vary for each call (to conserve battery), so you can now press the button several times in a row. And a check to verify that the host application is reachable.

2.) In the ViewController, remember to update the GUI on the main thread:

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
DispatchQueue.main.async {
self.lablel.text = "Message : \(message)"
}
}

Otherwise the label will not update when you receive the data.

Let me know if it helps you!

Share Data From Apple Watch to iPhone

If the watch app is not running it can't send a message to the iPhone app, and it cannot be launched from the iPhone app, so I don't know if your idea will make sense.

But you can send a message from a running watch app to the iPhone using the WCSession class functions.

https://developer.apple.com/documentation/watchconnectivity/wcsession

transferUserInfo is one function that may do what you want

swift how to share data between Watch and iPhone on background

In short, you should use the updateApplicationContext method instead of the sendMessage to be able to send data from the iPhone app even when the Watch app is in background. For further info, please continue on.

If you look at the documentation, it states that the calling session.sendMessage doesn't wake the Watch app if it is running only in the background.

Calling this method from your WatchKit extension while it is active
and running wakes up the corresponding iOS app in the background and
makes it reachable. Calling this method from your iOS app does not
wake up the corresponding WatchKit extension. If you call this method
and the counterpart is unreachable (or becomes unreachable before the
message is delivered), the errorHandler block is executed with an
appropriate error.

It also states that this function only works if the isReachable is true.

Use the sendMessage(:replyHandler:errorHandler:) or sendMessageData(:replyHandler:errorHandler:) method to transfer data
to a reachable counterpart. These methods are intended for immediate
communication between your iOS app and WatchKit extension. The
isReachable property must currently be true for these methods to
succeed.

For sending data that is used to update the UI, you should use the updateApplicationContext(_:) method, using which

The system sends context data when the opportunity arises, with the
goal of having the data ready to use by the time the counterpart wakes
up.

For this method to work, the session only needs to be activated, it doesn't need to be reachable.



Related Topics



Leave a reply



Submit