Now that we have basic idea of WatchKit App and various components of a WatchKit App, let us build a simple Hello World App to understand WatchKit App better.
We are going to build a simple app that displays the text “Hello World!” and displays current time on the Apple Watch.
First step is to ensure that your Xcode version is >= 6.2. Note that WatchKit is not supported prior to Xcode 6.2. Also keep in mind that WatchKit is an extension to iOS Apps; therefore we require an iOS App that also has WatchKit as target.
As the requirement is clear now, our Step 1 would be to start with creating a new Xcode project just like any other app.
Prefer the language to be swift, as we all know Apple is vouching for it now.
Next Step is a new entry to the process. As told earlier, we need to build iOS app that also targets WatchKit. To do this, select File -> New -> Target
Keep an eye on the various differences you see from your regular app building. These are the ones that define how a WatchKit targeted app works.
Observe and understand the differences in project structure (highlighted in the snapshot below). Two main things to note are the additional targets and also the new folders in project structure.
You can see that WatchKit Extension and WatchKit App targets are added. Also you can see that there are corresponding folders added in the project structure.
WatchKit App folder will only have the UI files (storyboard and assets folder) of the app that runs on the Apple Watch. All the corresponding controllers are written in the WatchKit Extension folder. These are executed on the iOS device that is paired with our watch.
Building Hello World
The UI we want on the watch should be built on the storyboard present in WatchKit App folder. Note that we only have interface controller for the UI of App and not the traditional View Controller.
Navigate to HelloWorld WatchKit App Folder -> Interface.storyboard. Drag and drop a label – for our text “Hello World!” and Date – for displaying date.
Observe the indication “Main”. It says that this is the starting point of our WatchKit App UI.
We have our Hello World WatchKit App ready now! As simple as it sounds!
Running the WatchKit App
Before we go ahead and close this post, let us understand what happens when you build and run this app.
As you seen, we have three things here-
- iOS Application
- WatchKit extension
- WatchKit application
iOS Application and WatchKit extension run on iOS device and the WatchKit application runs on the paired Apple Watch. The WatchKit application can run only if its WatchKit extension is running on the paired iPhone.
Below is the output of our Hello World WatchKit App
If you do not see the Apple Watch Simulator, navigate to Hardware -> External Displays -> Apple Watch (You can see two variants – use any of your choice).
We have seen how to build a simple WatchKit App, the new components that are added to our project structure.