All Blog Posts

Apple Watchkit: A beginner's guide

The Apple Watch is coming, so it’s probably time you learnt how to create a WatchKit app in xCode! Make sure you have the latest xCode beta installed or if you’re reading this many months later and WatchKit has officially been released then make sure you have the latest version of xCode.

Development

apple watch hello world

Let's get started!

Step 1:

Open xCode select File > New Project > Single View Application

Single view application

Step 2:

Name your application appropriately and finish the project setup.

Now time to create the WatchKit part of your app. Go to File > New Target > iOS > Apple Watch > WatchKit App. (If a modal comes up click the Activate button).

watchkit app

Step 3:

You will see that xCode has created two new folders. The first folder is your WatchKit extension this is where all your code goes, the second is your WatchKit app where the app interface is managed. As you can see the extension contains an initial interface controller and the app contains a storyboard file.

first watchkit project

Step 4:

Lets add something to our first app! Go into the Interface.Storyboard and see the initial interface controller. Drag a label from the object menu in the bottom right hand corner onto the interface.

label watchkit

Step 5:

Lets position the label in the middle of the interface. Click on the label and set the vertical and horizontal position to centre.

position watchkit

Step 6:

Time to link the label up to the InterfaceController. Open up the InterfaceController.h file on the right and control drag from the label to the file (this should feel fairly familiar if you are already using storyboards). Name the label and see your new outlet.

naming label watchkit

Step 7:

Dive into the InterfaceController.m and see three default WKInterfaceController methods.

– (void)awakeWithContext:(id)context

AwakeWithContext is the equivalent to init in a normal view controller, context is an id of an object that can be parsed into the controller when it is initialised.

– (void)willActivate

WillActive is the equivalent of ViewWillAppear in a UIViewController.

– (void)didDeactivate

DidDeactivate is the equivalent of ViewDidDisappear in a UIViewController.

In the WillActivate method set the label text.

[self.myFirstLabel setText:@“Hello World!”];

 

Step 8:

To run the WatchKit app select it in the xCode target drop down and click Run.

run watchkit app

Step 9:

Navigate to the simulator and see your first Watchkit app running! (Pro tip: if you get stuck in infinite loading indicator mode where the loading indicator never goes away, just stop and start the simulator 3 times the third run will work……beta software).

hello world

What next?

Have a play with your new project and see what you can make! I will be writing more Dev tutorials on creating WatchKit apps covering WKInterfaceTable, WKInterfaceGroup, WatchKit Navigation and hopefully many more.

Published on February 11, 2015, last updated on May 31, 2018

Give us the seal of approval!

Like what you read? Let us know.

0

Tags