I have been trying for multiple hours to find a way to send a global variable from the Apple Watch to the iPhone.
There are a lot of questions abut transferring data from iPhone to Apple Watch but not vice versa in Swift 3.
This is not a duplicate because I want to transfer data from the Apple Watch to iPhone, not vice Versa.
How can I do this?
Watch Connectivity allows you to send data between your Watch app and its companion iPhone app when both devices are within Bluetooth range or on the same Wi-Fi network.
Tap on your profile icon in the top right corner. Scroll down to the bottom of the Health profile and tap on “Export Health Data” Tap on “Export” to confirm that you want to export.
While you're on a call Switch a call from your Apple Watch to your iPhone: While talking on your Apple Watch, unlock your iPhone, then tap the green button or bar at the top of the screen.
Transferring data from Apple Watch to iPhone is very similar to vice versa.
For global variable you probably should use updateApplicationContext()
of WCSession
:
let session = WCSession.default()
if session.activationState == .activated {
session.updateApplicationContext(["my_global": g_myGlobal])
}
On the iPhone you should assign delegate to the default WCSession
and activate it. In the WCSessionDelegate
, implement the following method:
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
let receivedGlobal = applicationContext["my_global"] as? TypeOfTheGlobal
}
Alternatively you can use sendMessage(_:replyHandler:errorHandler:)
but this will require iPhone to be reachable.
In general I would recommend you to follow Zak Kautz advice and read about WatchConnectivity. This is the most common way to communicate between watch and phone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With