Wearable Watch App Sync with Mobile Device

Wearable Watch App Sync with Mobile Device
What are you going to learn?

Built a little interface for Apple Watch using WKInterfaceController. Transferring data from iPhone to Apple Watch is very similar to vice versa. However, in this demo, we will add a UITableView to the iPhone app, and update the WKInterfaceTable on the watchOS app.

Let’s Understand what is WKInterfaceController?

A class that provides the infrastructure for managing the interface in a watchOS app. An interface controller serves the same purpose as a UIViewController object in a UIKit app, except that it does not manage any actual views. It runs in your WatchKit extension and remotely manages the behavior associated with an interface controller in your Watch app’s storyboard file. Subclass WKInterfaceController use its methods to configure the elements of your storyboard scene and to respond to interactions with those elements.

Let’s dive programmatically

We are going to create a new project. Select the template for ‘iOS App with WatchKit App’ and follow basic steps to create it.

Let’s dive programmatically
What is App group?

An App cannot access data outside the sandbox due to privacy and security reasons. To support App extensions, Apple provides shared resources called App group. App Group only works when we enable it in Containing App, App extension as well as App Developer Portal.

How to create App group?

Select App or App Extension => signing & capabilities => Click +

How to create App group?How to create App group?
Apple Watch Interface

Now, we are going to create a little interface for the Apple Watch composed by WKInterfaceTable . Next, inside the Table, we are going to insert WKInterfaceLabel and set the number of lines to 0.

Here, WKInterfaceTable serves the same purpose as a UITableView object and WKInterfaceLabel serves the same purpose as a UILabel in a UIKit app.

Apple Watch Interface
Set up the watchOS App

Now, connect the Table from Interface Builder to code in InterfaceController. So, we first need to import the WatchConnectivity framework.

Next, we will use WCSession.default and classes, however we need to add delegate method activationDidCompleteWith . WCSession.default is mainly used for transferring data from iPhone to Apple Watch or Apple watch to iPhone.

How to Display Content on WKInterfaceTable ?

We will use setNumberOfRows for multiple rows and for loop is used to set Content on WKInterfaceTable .

For receiving the data in Apple watch, we will use didReceiveMessage .

Setup the iOS App

Firstly, in the iOS app’s UIViewController , we need to set up few things:

So, we first need to import the WatchConnectivity framework. Without that, nothing else we do would work. Next, to respond to callbacks from the WCSession , we need to set this ViewController as the WCSession’s delegate, and to do that we need it to confirm to the WCSessionDelegate protocol, so add that after the ViewController’s UIViewController superclass declaration.

To set up the session, first we check the response from WCSession’s "isSupported" method. This tells our code whether it even handles sessions. This is particularly important if this code is run on an iPad. You can’t currently pair an Apple Watch to an iPad, so this would respond with a false, and you should just not run any Watch Connectivity code at all. On the iPhone though, this would respond with true.

Add UITabelview Delegates and Datasource method
Add TextField in Alert Controller
Pass data to Apple Watch
Receive data on Apple Watch And set data on Table

There you go, we successfully build an iOS app to show real-time functionalities between iPhone app and Apple watch.