Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transfer Data From Apple Watch To iPhone in Swift [closed]

Tags:

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?

like image 253
Dylan Murphy Avatar asked Jun 21 '17 16:06

Dylan Murphy


People also ask

Can you transfer data from Apple Watch to iPhone?

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.

Can you pull data from Apple Watch?

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.

How do I switch from watch to iPhone?

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.


1 Answers

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.

like image 168
kelin Avatar answered Oct 11 '22 17:10

kelin