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.
Open xCode select File > New Project > Single View Application
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).
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.
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.
Lets position the label in the middle of the interface. Click on the label and set the vertical and horizontal position to centre.
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.
Dive into the InterfaceController.m and see three default WKInterfaceController methods.
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.
WillActive is the equivalent of ViewWillAppear in a UIViewController.
DidDeactivate is the equivalent of ViewDidDisappear in a UIViewController.
In the WillActivate method set the label text.
[self.myFirstLabel setText:@“Hello World!”];
To run the WatchKit app select it in the xCode target drop down and click Run.
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).
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 April 9, 2019