Communication between Apple Watch and iPhone app

The aim of this post is to show a possible mechanism for implementing the communication between Apple Watch and iPhone app. After trying several methods: CoreData, NSKeyedUnarchiver and NSUserDefaults, I have decided to chose NSUserDefault because it was the most simplified one. I would recommend the other two just in case that you wanted to share bigs amounts of data.

The application that I am going to create will show in the watch screen the value of a counter, that is increased (or decreased) from the iPhone app. Before continuing, be sure that you are working with Xcode 6.2, otherwise you will not be able to create a watchkit extension.

Create a new Single View project

Create a new Single View Application project:

Communication between Apple Watch and iPhone app

Communication between Apple Watch and iPhone app

 

Go to project properties>Capabilites>App Group, turn it on and create a new group:

Communication between Apple Watch and iPhone app

 

In the iPhone app storyboard we will add another label and a stepper:

Communication between Apple Watch and iPhone app

 

Add an action method for the stepper and leave it in blank by the moment, we will fill it later.

Communication between Apple Watch and iPhone app

The watch kit extension

Go to File>New>Target…>iOS>Apple Watch

Communication between Apple Watch and iPhone app

Communication between Apple Watch and iPhone app

 

And go to project properties, select the target extension (E), select Capabilities, turn on App Groups and select the group that you created before:

Communication between Apple Watch and iPhone app

 

Now we will place the label in the watch kit storyboard that will show the value of the counter.

Communication between Apple Watch and iPhone app

 The common data

I have created a new group on top the project hierarchy and added a module called Model, that is where I will place the common data.

Communication between Apple Watch and iPhone app

 

VERY IMPORTANT: Be sure that those files are assigned for both targets:

Communication between Apple Watch and iPhone app

 

Now is time to fill the stepper action method that we have created previously and do not forget the #import:

Now let make an stop in the path for checking if stepper is updating the counter. Select watchkitCounter target and any iPhone simulator and press the play button for running the app in the iPhone:

Communication between Apple Watch and iPhone app

 

Click on the stepper for assuring that label updates with counter value:

Communication between Apple Watch and iPhone app

 

Next step is to fill in the watch kit extension interface controller:

 

I have initialised the label when the form is loaded and program a timer for pulling the data. Finally build and execute the target watchkitCounter Watch App:

Communication between Apple Watch and iPhone app

 

Once the simulator is on you will see that the app is not running, this is normal. Be sure that apple watch simulator is also on screen, if does not go to iOS Simulator>Hardware>External Displays>Apple Watch (and re-run):

Communication between Apple Watch and iPhone app

 

Do not get nervous, if you see blank screen on watch kit simulator, on my computer (Mac-mini 16GB+SSD) takes almost 10 seconds for starting up. Once is on, you will see that watch-simulator refreshes the value of the counter, but the iPhone simulator does not start up the iPhone app, next step is select the iPhone app play with the stepper and observe how counter in the watch-simulator is being updated.

Communication between Apple Watch and iPhone app

 

And that is all!. In case that you had any doubt or something were not properly explained, you can download the demo project from here. I hope that this post would be useful and clarified your doubts, please feel free to send me any comment or amend.

 

4 thoughts on “Communication between Apple Watch and iPhone app

  1. Pingback: Image animations on Watchkit sdk - CeleriesCeleries

  2. Pingback: Integrating FontAwesome on watchkit - CeleriesCeleries

  3. As you said,” if you see blank screen on watch kit simulator, on my computer (Mac-mini 16GB+SSD) takes almost 10 seconds for starting up.” But I was waiting long time, I can’t see whether both(iphone+watch) is running. Can you please help me this. I am using Mac-mini 4GB Ram. Can we run both simulators at a time?

Leave a Reply

Your email address will not be published.