Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - WatchKit how to send message/data from iPhone app to WatchKit app?

I am creating a WatchKit app and was wondering how to send a message/data from the iPhone to the Watch?

I know how to do it the other way around (watch -> phone) using 'openParentApplication:reply:' and 'application:handleWatchKitExtensionRequest:reply:' but can't find any documentation on how to communicate from phone to watch.

Simple setup would be the iPhone app has a button that when pressed should update a label on the Watch app.

Can anyone point me in the right direction?

like image 415
JimmyJammed Avatar asked Mar 17 '23 12:03

JimmyJammed


1 Answers

First, you have to enable app groups for your target:

enter image description here

Then you can start to write and read objects via NSUserDefaults:

// write 
let sharedDefaults = NSUserDefaults(suiteName: appGroupName)
sharedDefaults?.setInteger(1, forKey: "myIntKey")


// read
let sharedDefaults = NSUserDefaults(suiteName: appGroupName)
let myIntValue = sharedDefaults?.integerForKey("myIntKey")

See the chapter Sharing Data with Your Containing iOS App in Apple Watch Programming Guide: Developing for Apple Watch

like image 129
zisoft Avatar answered Apr 24 '23 22:04

zisoft