Getting Started with Apple WatchKit and WatchOS 2

Continued from page 1. 

Apple has also provided documentation on the new Extension Delegate/WKExtension class and WKExtensionDelegate, which is central for developers in managing the lifecycle events of the watch app, as well as in managing data sharing between devices. The following diagram outlines the data lifecycle of watchOS apps:

image: ../Art/wkextension_states_2x.png

(source: Apple Development)

The new paradigm of data-sharing between iOS and watch is part of of the Watch Connectivity framework, which we will now examine:

Watch Connectivity

With the Watch Connectivity framework, as we just mentioned, data is passed back between the watch and iOS. Depending on whether both apps are in the foreground, data is passed live or in background, similar to background processing that developers are familiar with when working on their iOS apps.

WKSession is the primary conduit class between iOS and watchOS, with developers utilizing the singleton activeSession() method to reference its session instance and work with session objects, which should implement the WSessionDelegate to allow your apps to receive messages sent by their counterparts (by first establishing a session between the devices).

Transfering files then can be established via the use of one of the following available methods:

  • updateApplicationContext:error - for minor transfers, such as dictionaries of data, which will be received on the watch device when the watch wakes up (not necessarily in the counterpart app), available at launch of app and available on the watch via the corresponding :didReceiveUpdate: method.
  • sendMessageData(:replyHandler:errorHandler:) - transfers data to the watch and vice-versa immediately, and forces the iOS app to awake (and run in the background). Or, if the watch is the target device, it will return an error if the watch app isn’t running. This method should therefore ensure the watch app is running and active. The counterpart (target) delegate method is didReceiveMessageData:replyHandler.
  • transferFile(:metadata:) is utilized for larger file sets, such as images, and is done in the background to ensure no interruption to the main application threads. As with the previous method, it would also force the iOS app to awake and run in the background, or return an error if the watch app (as the target) is not active and in the foreground.

Connectivity is a complex topic in itself, and it’s recommended that developers cite Apple’s documentation, as well as the relevant WWDC 2015 sessions, for a complete understanding of this new architecture.


Animation is another area that has been significantly improved.

With WatchOS 1, developers had to be content with animation consisting of a pre-defined sequencing of a series of images to create the perception of animation. WatchOS 2 and WKInterfaceController now support basic animation constructs through animateWithDuration(:animations), which brings developers closer to what they expect in iOS.

Taking a duration and block, developers can now animate UI element objects over a provided duration. In addition to position, properties such as alpha, color, tints and insets can be animated.

Other UI Improvements


First of all, we now get a whole new UI element, WKInterfacePicker, which provides a list of items that is scrollable via the Digital Crown or finger gestures.


You would have seen this previously in WatchOS 1, when you customized your watch face complications. Third-party developers can now harness the same UI element on their own apps.


(source: Apple)

In addition to text, developers can add images or a mix of both text and images. This can be presented through a few different options and styles, such as list, stacked and image sequences.



We also get new alerts, which can now also be presented as action sheets. Presenting a standard way of alerting and prompting users to select from a specific set of options, you allow for both interruptive and tacit forms of engagement UI elements.


Presenting an alert or actions sheet can be accomplished by simply calling the presentAlertControllerWithTitle:message:preferredStyle:actions: method, suppling the title and message, as well as buttons you want to include.

Audio and Video

Now developers can provide Apple Watch users with the ability to play audio or video content directly on their wrists, as well as the ability to record audio. Audio and Video clips should always be short (a few seconds) to preseve battery and usability.

Using the presentMediaPlayerControllerWithURL:options:completion: method, the interface controller will be presented with a media playback interface (for either audio or video). Developers have the choice of either displaying a poster image of the media, or buttons or table rows, as a way of triggering playback of the content.

Passing in the WKMediaPlayerControllerOptionsAutoplayKey option causes the movie to begin playing as soon as it’s ready. The media player handles all user interactions and provides a Done button that the user can use to dismiss the interface. You do not have to dismiss the interface explicitly in your completion handler block. (Source: Apple)

Recording audio clips cn be done through the presentAudioRecordingControllerWithOutputURL:preset:maximumDuration:actionTitle:completion: method, which displays a standard recording interface. This results in content that will be saved in a location specified by the user. The user needs to explicitly accept the recording before saved to the device.

Hardware & Sensor Accessibilities

Being able to gain more direct access to the Apple Watch hardware is a boon for developers, and being able to tap into sensors such as the Taptic Engine and Heart Rate feature, as well as hardware components such as the Digital Crown and Accelerometer and Microphone, will bring about a whole new class of exciting and powerful apps.


Gaining direct access to the Watch’s accelerometer provides developers with the ability to create motion-based apps, or apps that are capable of tracking a user’s accelerating (as well as decelerating) motions. Think of an app that can track a user as he is swinging his golf club, or a tennis player swinging her racket, and gathering data to provide technique pointers.

Be sure to read the next Mobile article: Code Sign Your Apps on iOS with Less Fuss and Muss


Comments (0)