Apple Watch Programming Guide
Apple Watch Programming Guide
Contents
Overview 7
WatchKit Apps 26
App Essentials 27
How Interface Controllers Work 27
Assembling Your Storyboard Scenes 28
Accommodating Different Display Sizes 29
Updating Your Interface at Runtime 32
Setting Your App’s Key Color 32
Internationalizing Your Interface 33
Interface Navigation 34
Implementing a Page-Based Interface 34
Implementing a Hierarchical Interface 35
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
2
Contents
Interface Objects 37
Creating an Interface Object 37
Configuring Your Interface at Design Time 38
Changing Your Interface at Runtime 39
Responding to User Interactions 40
Hiding Interface Objects 41
Images 46
Specifying Your Image Assets 46
Using Named Images to Improve Performance 46
Caching Images on the Device 47
Tables 48
Configuring Row Controllers 48
Configuring the Table’s Contents at Runtime 50
Handling Row Selections 52
Context Menus 53
Designing Your Menu Items 53
Adding a Context Menu to an Interface Controller 54
Handling Taps in a Menu Item 55
Glances 56
Glance Essentials 57
The Glance Life Cycle 58
Glance Interface Guidelines 58
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
3
Contents
Notifications 62
Notification Essentials 63
The Short-Look Interface 64
The Long-Look Interface 65
Adding Action Buttons to Notifications 67
Responding to Taps in Action Buttons 68
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
4
Figures, Tables, and Listings
App Essentials 27
Figure 5-1 Interface objects in Xcode 28
Figure 5-2 Customizing attributes for different devices 31
Interface Objects 37
Figure 7-1 Configuring a label object 38
Table 7-1 Action methods for interface objects 40
Listing 7-1 Initializing an interface controller in Objective-C 39
Listing 7-2 Initializing an interface controller in Swift 39
Tables 48
Figure 10-1 Examining a row controller in Xcode 50
Listing 10-1 Sample class for managing a row 49
Listing 10-2 Creating and configuring the rows of a table 51
Context Menus 53
Figure 11-1 A context menu with three items 53
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
5
Figures, Tables, and Listings
Glance Essentials 57
Figure 12-1 Glance interface for the Lister sample app 57
Notification Essentials 63
Figure 14-1 A short-look interface 65
Figure 14-2 A long-look notification interface 66
Listing 14-1 Registering actions in a containing iOS app 67
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
6
Overview
Important: This is a preliminary document for an API or technology in development. Apple is supplying
this information to help you plan for the adoption of the technologies and programming interfaces described
herein for use on Apple-branded products. This information is subject to change, and software implemented
according to this document should be tested with final operating system software and final documentation.
Newer versions of this document may be provided with future betas of the API or technology.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
7
Developing for Apple Watch
Apple Watch offers users a distinctly personal and unobtrusive way to access their data. Rather than pull an
iPhone out of a pocket, a user can glance at Apple Watch and get important information quickly. Apps written
for Apple Watch support these brief interactions by providing only the most relevant information in the most
straightforward way possible.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
8
Developing for Apple Watch
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
9
Developing for Apple Watch
The WatchKit App
Apple Watch requires the presence of an iPhone to run third-party apps. Creation of a third-party app requires
two separate bundles: a WatchKit app that runs on Apple Watch and a WatchKit extension that runs on the
user’s iPhone. The WatchKit app contains only the storyboards and resource files associated with your app’s
user interface. The WatchKit extension contains the code for managing the WatchKit app’s user interface and
for responding to user interactions.
The user interacts with your WatchKit app in the following ways:
● The user interacts with your app’s full interface by launching the app from the home screen. Your app’s
full interface can have multiple screens of content and can facilitate interactions with your app’s data. You
must always provide a full app experience.
● A Glance is a read-only interface that you use to display the most timely and relevant information from
your app. Providing a glance is optional.
● Custom notification interfaces you change the way local and remote notification content is displayed to
the user. Use a custom interface to incorporate graphics, additional content, and a custom layout to your
notification content. Providing custom notification interfaces is optional.
Because a WatchKit app extends the behavior of your existing iOS app, the WatchKit app and WatchKit extension
are bundled together and packaged inside your iOS app bundle. During installation of your iOS app, the system
prompts the user to install the WatchKit app when a paired Apple Watch is present.
A WatchKit app acts as the public face of your app but it works in tandem with your WatchKit extension, which
is the brains of the operation. The WatchKit app contains only the storyboards and resource files associated
with your app’s user interface. The WatchKit extension contains the business logic and code for managing
content, responding to user interactions, and updating your user interface. And because the extension runs
on the user’s iPhone, it can coordinate with your iOS app as needed to perform more sophisticated tasks.
To get started creating a WatchKit app, see App Essentials (page 27).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
10
Developing for Apple Watch
Glance Interfaces
Glance Interfaces
A Glance is a focused interface that you use to display your app’s most important information. Glances are
aptly named because they are intended to be looked at quickly by the user. Glances do not scroll; the entire
Glance interface must fit on a single screen. Glances are read-only and cannot contain buttons, switches, or
other interactive controls. Tapping a glance launches your WatchKit app.
Creating a Glance interface does not require creating a separate executable. Creating a Glance involves creating
a specialized set of objects inside your existing WatchKit app and WatchKit extension. In fact, the classes and
techniques you use to implement a Glance are the same ones you use to create your WatchKit app.
To get started creating a glance interface, see Glance Essentials (page 57).
Apple Watch provides automatic support for the actionable notifications introduced in iOS 8. Actionable
notifications are a way to add buttons to your notification interface that reflect actions the user might take.
For example, a notification for a meeting invite might include buttons to accept or reject the invitation. When
your iOS app registers support for actionable notifications, Apple Watch automatically adds appropriate buttons
to the notification interfaces on Apple Watch. All you need to do is handle the actions selected by the user in
your WatchKit extension.
To get started with creating a custom notification interface, see Notification Essentials (page 63).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
11
Configuring Your Xcode Project
Creating a WatchKit app requires an existing iOS app. In the Xcode project for your iOS app, you add a new
WatchKit app target, which configures the bundles and initial resources for your WatchKit app and WatchKit
extension. Those bundles are then delivered as part of your iOS app on the App Store.
The WatchKit app target provided by Xcode contains everything you need to get started creating your WatchKit
app, glances, and custom notification interfaces. And iOS Simulator provides you with a runtime environment
for testing the appearance and behavior of all of your interfaces.
To add a WatchKit app target to your existing iOS app project, do the following:
1. In Xcode, open the project for your iOS app .
2. Select File > New > Target and navigate to the Apple Watch section.
3. Select WatchKit App.
4. Click Next.
5. If you plan to implement a Glance or custom notification interface, select the appropriate checkboxes.
For notification interfaces, it is recommended that you enable the Include Notification Scene checkbox,
even if you do not plan on implementing that interface right away. Selecting that checkbox adds an
additional file to your project for debugging your notification interfaces. If you do not select that option,
you must create the file manually later.
6. Click Finish.
Xcode configures the targets for your WatchKit app and WatchKit extension and adds the needed files to your
iOS project. The bundle IDs for both new targets are configured automatically and are based on the bundle
ID of your iOS app. The base IDs for all three bundles must match; if you change your iOS app’s bundle ID, you
must update the other bundle IDs accordingly.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
12
Configuring Your Xcode Project
App Target Structure
Figure 2-1 illustrates the structure of your iOS app and WatchKit executables. The WatchKit app is packaged
inside your WatchKit extension, which is in turn packaged inside your iOS app. When the user installs your iOS
app on an iPhone, the system prompts the user to install your WatchKit app if there is a paired Apple Watch
available. The installation process is handled automatically by iOS and requires no further work on your part.
For apps that include Glance or custom notification interfaces, you must configure additional build schemes
to test those interfaces. Use the glance scheme for debugging your glance interface in the simulator and use
the notification scheme to test your dynamic and static notification interfaces.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
13
Configuring Your Xcode Project
The Build, Run, and Debug Process
3. Duplicate your existing WatchKit app scheme and give the new scheme an appropriate name.
For example, give it a name like “Glance - My WatchKit app” to indicate that the scheme is specifically
for running and debugging your glance.
4. Select the Run option in the left column of the scheme editor.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
14
Configuring Your Xcode Project
Specifying a Notification Payload for Testing
5. In the Info tab, select the appropriate executable for the new scheme.
When creating a build scheme for your notification interfaces, you can also specify a JSON file to use as the
notification payload during testing. For more information about notification payloads, see Specifying a
Notification Payload for Testing (page 15).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
15
Configuring Your Xcode Project
Specifying a Notification Payload for Testing
Note: If you enabled the Include Notification Scene option when creating your WatchKit app target,
Xcode provides you with an initial PushNotificationPayload.apns file for specifying your test
data. The file is located in the Supporting Files directory of your WatchKit extension. You can also
create payload files manually later.
The PushNotificationPayload.apns file contains most of the keys you need to simulate a remote
notification, and you can add more keys based on your needs. Figure 2-2 shows the default JSON file that
comes with your project.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
16
Configuring Your Xcode Project
Specifying a Notification Payload for Testing
Most, but not all, of the JSON data is packaged into a dictionary and delivered to your code at runtime. Because
iOS Simulator does not have access to your iOS app’s registered actions, payload files may also specify the
action buttons to display in your interface. The “WatchKit Simulator Actions” key contains an array of dictionaries,
each of which represents an action button to add to your interface. Each dictionary contains two keys:
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
17
Configuring Your Xcode Project
Specifying a Notification Payload for Testing
● title - The value of this key is the title of the action button.
● identifier - The value of this key is the string to pass to your interface controller’s
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
or application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
method.
● destructive - The value of this key is the value 1 or 0, where 1 causes the resulting button to be
rendered in a way that indicates it performs a destructive action. The value 0 causes the button to be
rendered normally. Inclusion of this key is optional.
To test your notification interface with the JSON payload, configure the build scheme with the appropriate
payload file. When you select a notification interface executable, Xcode adds a menu for selecting one of your
payload files. You can create different build schemes for different notification payloads or you can update the
payload file for an existing build scheme prior to testing.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
18
WatchKit App Architecture
Apps built for Apple Watch consist of two parts: a WatchKit app and a WatchKit extension. The WatchKit app
resides on the user’s Apple Watch and contains your storyboard and resource files but no code. The WatchKit
extension resides on the user’s iPhone (inside your containing iOS app) and contains the code and resource
files for managing your WatchKit app’s interface.
The WatchKit app and WatchKit extension work in tandem to implement your app’s interface. When the user
interacts with your app on Apple Watch, your WatchKit app chooses the appropriate scene from your storyboards
to handle that interaction. For example, if the user views your app’s Glance, it chooses your Glance scene. After
choosing the scene, WatchKit tells the paired iPhone to launch your WatchKit extension and create the objects
needed to manage that scene. When the scene is fully configured, it is displayed on Apple Watch. The transfer
of information between the WatchKit app and WatchKit extension happens transparently behind the scenes.
Each scene is managed by a single interface controller object, which is an instance of the
WKInterfaceController class. An interface controller in WatchKit serves the same purpose as a view
controller in iOS: it presents and manages content on the screen and responds to user interactions with that
content. Unlike a view controller, an interface controller does not manage the actual views of your interface.
Those views are managed for you behind the scenes by WatchKit.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
19
WatchKit App Architecture
WatchKit App Life Cycle
WatchKit apps typically contain multiple interface controllers, where each interface controller displays a different
type of information. Because only one interface controller at a time is displayed onscreen, an app presents
new interface controllers in response to user actions. Apps can present interface controllers modally. The
navigation style of an app also determines how interface controllers are presented. For information on how
to present new interface controllers, see Interface Navigation (page 34).
Note: Glance and custom notification interfaces use specialized interface controllers that are separate
from your app’s other interface controllers. For information about implementing a glance, see Glance
Essentials (page 57). For information about the implementing custom notification interfaces, see
Notification Essentials (page 63).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
20
WatchKit App Architecture
WatchKit App Life Cycle
At launch time, WatchKit automatically loads the appropriate scene for the current interaction. If the user views
your app’s glance, it loads the glance scene from your storyboard; if the user launched your app directly, it
loads the initial scene for your app. After it loads the scene, it asks the WatchKit extension to create the
corresponding interface controller object, which you use to prepare the scene for display to the user. Figure
3-2 shows the steps for this sequence.
Use your interface controller’s init and awakeWithContext: methods to load any required data, set the
values for any interface objects, and prepare your interface to be displayed. Do not use the willActivate
to initialize your interface controller. The willActivate method is called shortly before your interface is
displayed onscreen, so you should use that method only to make last minute changes. For example, you might
also use that method to start animations or start other tasks that should only happen while your interface is
onscreen.
While your interface controller is onscreen, user interactions are handled by your interface controllers custom
action methods. As the user interacts with tables, buttons, switches, sliders, and other controls, WatchKit calls
your action methods so that you can respond. You use those action methods to update your interface or
perform other relevant tasks. To perform tasks at other times, use an NSTimer object to run code at the time
you designate.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
21
WatchKit App Architecture
Tasks to Perform at Different Stages of an App’s Life
Note: Glance interfaces do not support action methods. Tapping your app’s glance interface always
launches the app.
Your WatchKit extension remains running only while the user is interacting with your app on Apple Watch.
Interactions with Apple Watch are meant to be brief, so interface controllers should be lightweight and never
perform long-running tasks. When the user exits your app explicitly or stops interacting with Apple watch, iOS
deactivates the current interface controller and suspends your extension as shown in Figure 3-3.
init This method is your first chance to prepare your interface for display. Use it
to load data and update labels, images, tables, and other interface objects
in your storyboard scene.
awakeWithContext: This method lets you configure the interface controller using any available
context data. The context data is data you provide to assist in the
configuration of a new interface controller. For example, when pushing a
new interface controller in a hierarchical interface, you specify a context
object that contains the next level of data to display. Providing a context
object is recommended but not required.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
22
WatchKit App Architecture
Sharing Data with Your Containing iOS App
willActivate This method lets you know that your interface will soon be visible to the
user. Use this method only to make small changes to your interface. For
example, you might use this method to update a label based on new data.
The bulk of your interface initialization should still occur in the init and
awakeWithContext: methods.
didDeactivate Use the didDeactivate method to clean up your interface and put it into
a quiescent state. For example, use this method to invalidate timers and stop
animations.
You cannot set values for any interface objects from this method. From the
time this method is called to the time the willActivate method is called
again, any attempts to set values for interface objects are ignored.
You set up a shared app group from the Capabilities tab of your iOS app and WatchKit extension. Enabling the
App Groups capability adds an entitlements file (as needed) to each target and adds the
com.apple.security.application-groups entitlement to that file. To share data, both targets must
have the same app group selected.
At runtime, you share files between processes by reading and writing those files in the shared container
directory. To access the container directory, use the
containerURLForSecurityApplicationGroupIdentifier: method of NSFileManager to retrieve the
base URL for the directory. Use the provided URL to enumerate the directory contents or create new URLs for
files in the directory.
To share preferences data between apps, create an NSUserDefaults object using the identifier of the shared
group. The initWithSuiteName: method of NSUserDefaults creates an object that allows access to the
shared user defaults data. Both processes can access this data and write changes to it.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
23
WatchKit App Architecture
Communicating Directly with Your Containing iOS App
When you call the openParentApplication:reply: method, iOS launches or wakes up the containing iOS
app in the background and calls the application:handleWatchKitExtensionRequest:reply: method
of its app delegate. The app delegate uses the provided dictionary to perform the request and return a reply
to the WatchKit extension.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
24
Leveraging iOS Technologies
WatchKit extensions have access to the same technologies found in iOS apps, but because they are extensions,
use of some technologies may be restricted and the use of others is not recommended. Here are some guidelines
for deciding when to use a particular technology:
● Avoid using technologies that request user permission, like Core Location. Using the technology from
your WatchKit extension could involve displaying an unexpected prompt on the user’s iPhone the first
time you make the request. Worse, it could happen at a time when the iPhone is in the user’s pocket and
not visible.
● Do not use background execution modes for a technology. WatchKit extensions run only while the user
interacts with the corresponding WatchKit app and are therefore considered foreground extensions. As a
result, WatchKit extensions cannot execute using the background modes supported by some technologies.
● Avoid performing long-running tasks with a technology. A WatchKit extension is suspended soon after
the user stops interacting with the corresponding WatchKit app. Because WatchKit app interactions are
typically brief, the extension might already be suspended by the time the requested data arrives.
The best solution for performing any long-running tasks is to let your iOS app perform the task instead. For
example, instead of starting location services in your WatchKit extension, start it in your iOS app. Your iOS app
can gather the needed data and put it in a shared app group so that your extension can access it later. Use
the openParentApplication:reply: method to initiate tasks and receive a reply or use a shared group
container to communicate details between your iOS app and WatchKit extension. For information about how
to handle communication between your iOS app and WatchKit extension, see Communicating Directly with
Your Containing iOS App (page 24).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
25
WatchKit Apps
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
26
App Essentials
Xcode provides storyboard support for creating WatchKit app interfaces. When you add a WatchKit app to
your project, Xcode provides you with a storyboard for implementing your UI. The storyboard has at least one
scene for your WatchKit app’s interface and may have additional scenes for your glance or notification interfaces.
You can add additional scenes to your storyboard by dragging interface controller objects from the object
library.
Each new scene in your storyboard is backed by a corresponding WKInterfaceController subclass that
you provide. (For dynamic notification interfaces, you subclass WKUserNotificationInterfaceController
instead.) For every new scene, enter the name of your interface controller subclass in the Identity inspector.
All of your interface controller code goes in your WatchKit extension target. Do not include any code in your
WatchKit app.
There is no limit to the number of interface controllers you can have in your WatchKit app, but more interface
controllers usually means more complexity, so fewer is better. The navigation style you choose for your app
defines how users navigate from one scene to the next. Some navigation styles require you to explicitly present
the next interface controller while others do not. For more information on how to navigate between the scenes
of your app, see Interface Navigation (page 34).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
27
App Essentials
Assembling Your Storyboard Scenes
WatchKit also provides ways for you to position items more precisely within your storyboard scenes. The size
and position of most items can be configured using the Attributes inspector. Group objects offer another
important tool for positioning items, allowing you to lay out elements horizontally as well as vertically. Groups
also offer extra options for spacing items within the group bounds. Groups have no default visual representation
but can be configured with a background color or image if you want.
Figure 5-1 shows how different elements can be arranged in your storyboard file. The first three elements are
labels, each of which occupies the entire horizontal width of its line. For each label, the alignment value
determines whether the aligned to left, right, or center of interface controller’s bounds. Below the labels is a
group object containing two images arranged horizontally. The interface also contains a switch, a separator,
and a button all stacked vertically underneath the group object.
When creating your interfaces in Xcode, let objects resize themselves to fit the available space whenever
possible. App interfaces should be able to run both display sizes of Apple Watch. Letting the system resize
objects to fit the available space minimizes the amount of custom code you have to write for each device.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
28
App Essentials
Accommodating Different Display Sizes
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
29
App Essentials
Accommodating Different Display Sizes
To customize an item for a specific device size, use the plus buttons in the Attributes inspector to override the
value of an attribute for a given device. Clicking a plus button adds a new device-specific entry for the attribute.
Changes you make to that version of the attribute affect only the selected device. Figure 5-2 shows an example
where text scaling is handled differently for Apple Watch 42mm.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
30
App Essentials
Accommodating Different Display Sizes
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
31
App Essentials
Updating Your Interface at Runtime
Users should not notice any significant differences in your app’s interface on different sizes of Apple Watch,
so always minimize the customizations you make for different device sizes. Whenever possible, limit interface
changes to layout-related behaviors such as spacing and margins. Although it is possible to remove interface
objects altogether from your interface in different layouts, doing so is not recommended. You should always
try to use the exact same set of interface objects on all sizes of Apple Watch.
To see customizations applied to different device sizes, use the control at the bottom of the storyboard editor
to toggle between the device sizes. The storyboard editor displays the Any device size by default. Changes
applied in the Any display mode apply to all sizes of Apple Watch. If you change the display mode to a specific
device size, the changes you make while in that mode apply only to the current device.
You cannot add new objects to your interface or change the order of the objects that are already there. Although
you cannot remove objects, you can hide them, which removes them from the layout temporarily. When an
item is hidden, other objects fill in the space previously occupied by the item. To hide an object without filling
in the space, set the item’s alpha value to 0. For more information about hiding objects in a scene, see Hiding
Interface Objects (page 41).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
32
App Essentials
Internationalizing Your Interface
An app’s key color is stored in the Global Tint property of the app’s storyboard. To access this property, select
your storyboard and display the File inspector. Select one of several preexisting colors from the popup menu
or use the color picker to specify a custom color.
When thinking about internationalization, your main concern should be arranging your interface so that labels
and controls that contain text have room to expand. Instead of placing multiple buttons on the same line,
arrange them vertically so that each one has plenty of room to display its title.
For text and images that you specify programmatically, use the same internationalization techniques you use
for iOS and OS X apps.
● Use the NSLocalizedString macros to load strings from resource files.
● Use an NSNumberFormatter object to format numerical values.
● Use an NSDateFormatter object to format dates.
When used in your WatchKit extension, an NSLocale object returns the locale information configured on the
user’s Apple Watch. Use that class to get the user’s preferred languages and other language and locale-related
information.
For more information about internationalizing your app, see Internationalization and Localization Guide .
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
33
Interface Navigation
For WatchKit apps with more than one screen of content, you must choose a technique for navigating between
those screens. WatchKit apps support two navigation styles, which are mutually exclusive:
● Page-based. This style is suited for apps with simple data models where the data on each page is not
closely related to the data on any other page. A page-based interface contains two or more independent
interface controllers, only one of which is displayed at any given time. At runtime, the user navigates
between interface controllers by swiping left or right on the screen. A dot indicator control at the bottom
of the screen indicates the user’s current position among the pages.
● Hierarchical. This style is suited for apps with more complex data models or apps whose data is more
hierarchical. A hierarchical interface always starts with a single root interface controller. In that interface
controller, you provide controls that, when tapped, push new interface controllers onto the screen.
Apps can use modal presentations to supplement their base navigation style. Modal presentations are a way
to interrupt the current user workflow to request input or display information. You can present interface
controllers modally from both page-based and hierarchical apps. The modal presentation itself can consist of
a single screen or multiple screens arranged in a page-based layout.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
34
Interface Navigation
Implementing a Hierarchical Interface
The segues you create in your storyboard file define the page-based interface that is loaded when your app
is launched. You can change the set of pages you want to display by calling the
reloadRootControllersWithNames:contexts: method early in the launch cycle. For example, you might
call that method in the init method of your main interface controller to force WatchKit to load a different
set of pages.
All interface controllers in a page-based interface are created and initialized prior to displaying the interface,
but only one interface controller at a time is displayed. Normally, WatchKit displays the first interface controller
in the sequence initially. To change the initially displayed interface controller, call the becomeCurrentPage
method from its init or awakeWithContext: method.
As the user navigates from page to page, WatchKit activates and deactivates interface controllers accordingly.
During a transition, the currently visible interface controller’s didDeactivate method is called, followed by
a call to the willActivate method of the interface controller that is about to be displayed. Use the
willActivate method to update the contents of your interface controller to reflect any last minute changes.
When pushing a new interface controller onto the screen, it is recommended that you pass a data object in
the context parameter of the pushControllerWithName:context: method. This context object is how
you pass state information to the new interface controller before it appears onscreen. Use this object to tell
the new interface controller what data to display.
A pushed interface controller displays a chevron in the upper-left corner of the screen to indicate that the user
can navigate backward. When the user taps the upper-left corner of the screen or performs a left-edge swipe,
WatchKit dismiss the topmost interface controller automatically. You can also dismiss the interface controller
programmatically by calling its popController method. You cannot dismiss your app’s main interface
controller.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
35
Interface Navigation
Presenting Interface Controllers Modally
When creating a modal segue, connect the segue to the interface controller you want to display. When using
a segue to present multiple interface controllers, first use the next page segue to connect the modal interface
controllers together, in the same way that you connect them together for a page-based interface. Your modal
segue should connect to the first interface controller in the group. If you connect to an interface controller in
the middle of the group, the interface controllers that precede it in the group are not displayed.
The top-left corner of a modal interface displays the interface controller’s title string. When the user taps that
string, WatchKit dismisses the modal interface. Set the title string to reflect the meaning of dismissing the
modal interface. For example, when displaying information, you might set the string to Done or Close. If you
do not specify a title for your interface controller, WatchKit displays the string Cancel by default.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
36
Interface Objects
You manipulate your WatchKit app’s UI using interface objects. An interface object is an instance of the
WKInterfaceObject class, or more specifically one of its subclasses. The WatchKit framework provides
interface objects for most (but not all) of the visual elements you can add to your UI in your storyboard files.
Interface objects are not views. They are proxy objects that communicate wirelessly with the actual views used
to implement your UI on Apple Watch.
Note: Communication between an interface object and the corresponding view on Apple Watch is
one way, with information flowing from your WatchKit extension to Apple Watch. In other words,
you set values on an interface object but you cannot get the current values of its attributes. There
are performance and latency implications for retrieving data from Apple Watch, making changes,
and writing those changes back to the device. So it is recommended that you maintain information
about the configuration of your interface in your WatchKit extension.
When adding declared properties for interface objects, set the class to the appropriate type and include the
IBOutlet keyword in your declaration. For example, the declaration for a label is as follows:
@interface MyHelloWorldController()
@end
Connect each declared property in your interface controller to the corresponding item in your storyboard. A
quick way to create property declarations and connect them to an item is to use the Assistant editor in Xcode.
After displaying the Assistant editor, control-drag from an element in your storyboard to the interface definition
of your class to create an outlet. (In Swift, drag to your class definition.) After prompting you for the outlet’s
name, Xcode creates the property declaration in your class and connects it to the storyboard element.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
37
Interface Objects
Configuring Your Interface at Design Time
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
38
Interface Objects
Changing Your Interface at Runtime
For more information about how to configure interface objects, see the corresponding class descriptions in
WatchKit Framework Reference .
At initialization time, it is important to let WatchKit initialize your interface controller class before doing anything
else. The initialization methods of WKInterfaceController and its subclasses are where WatchKit creates
your app’s interface objects. So any initialization code you write for your interface controllers must call the
super implementation first. Listing 7-1 shows an example of an init method for an interface controller that
contains an outlet (called label) for a WKInterfaceLabel object.
- (instancetype)init {
if (self){
}
return self;
override init {
super.init
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
39
Interface Objects
Responding to User Interactions
To improve performance and battery life, the WatchKit framework optimizes any attempts to set values on
your app’s interface objects. Whenever you set the values for one or more interface objects in the same run
loop iteration, the new values are coalesced and transmitted to Apple Watch in a single batch to improve
efficiency. Coalescing changes means that only that last change to a given property of an object is sent to the
device. More importantly, setting the same property to the same value generates a log message to help you
track down the duplicate calls.
For information about the methods you use to configure your interface objects, see the corresponding class
descriptions in WatchKit Framework Reference .
Interfaces containing tables can use segues or the table:didSelectRowAtIndex: method of the interface
controller to respond to taps in a table row. Use a segue to display another interface controller. Prior to
performing the segue, WatchKit calls the contextForSegueWithIdentifier:inTable:rowIndex: or
contextsForSegueWithIdentifier:inTable:rowIndex: method of your interface controller so that
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
40
Interface Objects
Hiding Interface Objects
you can specify the context objects to use when displaying the interface controller. If you use the
table:didSelectRowAtIndex: method instead of a segue, you can perform whatever actions are appropriate
for tapping on the row.
After your interface controller is initialized and onscreen, WatchKit calls the methods of your interface controller
only when the user interacts with your interface. If you want to update your user interface without user
intervention, you must configure an NSTimer object and use its handler to perform any needed tasks.
For tasks that might take more than a second or two, consider handing those tasks off to your parent iOS app
for execution. Long-running tasks such as network access and location monitoring are best handled by the
parent app, which can then communicate that information back to your WatchKit extension through a shared
group container directory. For information about handing off tasks to your parent app, see Communicating
Directly with Your Containing iOS App (page 24).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
41
Text and Labels
To display text in your WatchKit app, use label objects. Labels support formatted text that can be changed
programmatically at runtime.
To add a label to your interface controller, drag it into the corresponding storyboard scene. From there, configure
the label’s initial text string and format. WatchKit supports both standard fonts and custom fonts that you
specify yourself. Figure 8-1 shows the standard font styles available for you to use.
For more information about configuring label objects in Xcode and in your code, see WKInterfaceLabel Class
Reference .
Using Fonts
WatchKit apps may use only the system font or custom fonts to display text. To use custom fonts, you must
install those fonts by doing the following:
● Include the custom font file in both your WatchKit app and WatchKit extension bundle.
● Add the UIAppFonts key to your WatchKit app’s Info.plist file and use it to specify the fonts you
added to the bundle. For more information about this key, see Information Property List Key Reference .
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
42
Text and Labels
Customizing the System Font
Important: You must include the font in your WatchKit extension so that you can create strings with that
font at runtime. The font information is included with the attributed string when it is sent to Apple Watch,
and the copy of the font in your WatchKit app bundle is then used to render the string there.
To format text using a custom font, create an attributed string using the font information and use that string
to set the text of your label, as shown in Listing 8-1. The font name and size are encoded with the attributed
string, which is then used to update the label on the user’s Apple Watch. If the font name you specify is not
the system font or one of your custom installed fonts, WatchKit uses the system font.
self.label.setAttributedText(attrString)
[UIFontDescriptorFeatureSettingsAttribute :
[UIFontFeatureTypeIdentifierKey : kLowerCaseType,
UIFontFeatureSelectorIdentifierKey : kLowerCaseSmallCapsSelector]])
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
43
Text and Labels
Managing Text Input
Listing 8-3 shows how to configure the text input controller and process the results. After specifying the initial
phrases and input modes, the controller runs asynchronously. When the user selects an item or cancels input,
your block is executed on the main thread. Use that block to retrieve the text or emoji image that was selected
by the user and update your app.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
44
Text and Labels
Internationalizing Your Text Code
[self presentTextInputControllerWithSuggestions:initialPhrases
allowedInputMode:WKTextInputModeAllowAnimatedEmoji
completion:^(NSArray *results) {
else {
}];
When internationalizing your app, your main concern should be arranging your interface so that labels (and
other controls with text) have room to expand. For example, rather than using a group to arrange three buttons
horizontally, arranging the buttons vertically gives each one room to grow horizontally when its text becomes
longer.
For more information about internationalizing your app, see Internationalization and Localization Guide .
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
45
Images
WatchKit provides the following ways to incorporate images into your content:
● The WKInterfaceImage displays a single image or a sequence of images as standalone content.
● The WKInterfaceGroup, WKInterfaceButton, and WKInterfaceController classes allow you to
specify an image as the background for other content.
Specifying images by name is preferred because the only the name string is transferred to Apple Watch, which
takes less time and uses less power than sending the entire image. WatchKit searches your WatchKit app bundle
for an image file with the name you specified. If it does not find an image file in the bundle, it then searches
the device-side image caches for an image with the given name.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
46
Images
Caching Images on the Device
Any time you create a UIImage object in your extension, that image object exists on the user’s iPhone and
must be sent to Apple Watch before it can be used. Even using the imageNamed: method of UIImage loads
the image from your WatchKit extension’s bundle, not from your WatchKit app. If you try to assign that image
to one of your interface objects, the image data is transferred wirelessly to Apple Watch.
The Apple Watch image caches are limited in size, with each app receiving approximately 20 MB of cache
space. Caches are persistent and can be used between launches of the WatchKit app. When your app’s caches
fill up, you must remove existing images from the cache before attempting to add new ones. Use the
removeCachedImageWithName: method to remove a single image or use the removeAllCachedImages
method to clear the caches completely.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
47
Tables
Use tables to display lists of data whose contents change dynamically. WatchKit supports single-column tables
using the WKInterfaceTable class. Displaying data in a table requires defining the layout for your data in
advance and writing code to fill the table with the actual data at runtime. Specifically, you need to do the
following in your Xcode project:
● In your storyboard file:
● Add a table object to your interface controller scene. Create an outlet for that table in your interface
controller.
● Configure one or more row controllers for your table as described in Configuring Row Types (page
48).
● In your code:
● Define a row controller class for each row controller you defined; see Configuring Row Types (page
48).
● At initialization time, add rows to the table as described in Configuring the Table’s Contents at
Runtime (page 50).
● Respond to interactions with table rows as described in Handling Row Selections (page 52).
For each table, you may define multiple row controller types, each with a different appearance. At runtime,
you specify which row types you need and in what order they should be arranged in the table. For additional
information about how to configure a table, see WKInterfaceTable Class Reference .
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
48
Tables
Configuring Row Controllers
Each row controller contains a single group element initially. To that group, you addd the labels, images, and
other objects that you want to include in the row. The content for labels and images in a row controller is
irrelevant at design time. At runtime, you replace the content of each item when you configure the row.
Each row controller is backed by a custom class that you use to access the row’s contents. Most row controller
classes contain only properties for accessing the row’s interface objects; few contain any code. However, if you
add buttons or other interactive controls to a row, your row class can also include action methods for responding
to user interactions with those controls.
Listing 10-1 shows a sample row controller class definition. In this example, the class contains outlets for an
image and a label.
@end
You finish the configuration of your row controller in your storyboard file by setting its class and connecting
up any outlets. You must also assign an identifier string to the row. You use that string at runtime when creating
the row.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
49
Tables
Configuring the Table’s Contents at Runtime
3. Set the class of the row controller to your custom class. Set this value in the Identity inspector.
4. Connect the labels and other elements to the outlets in your custom class.
Connecting items in your storyboard file to your outlets binds the two together. WatchKit needs this
information to create a row’s interface objects at runtime.
Figure 10-1 shows an example of a row controller configured with the identifier mainRowType and the class
MainRowType, which is defined in Listing 10-1 (page 49). The rowDescription and rowIcon outlets in that
class are connected to the image and label objects in the row.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
50
Tables
Configuring the Table’s Contents at Runtime
- (void)configureTableWithData:(NSArray*)dataObjects {
[theRow.rowDescription setText:dataObj.text];
[theRow.rowIcon setImage:dataObj.image];
When configuring tables, you can improve performance by limiting the number of rows you create initially.
Because table rows must all be created up front, creating large numbers of rows can adversely affect the
performance of your app. The precise number of rows depends on the complexity of your data and how long
it takes you to create each one, but consider keeping the total number of rows to 20 or fewer. For tables that
require more rows, consider loading only a subset of rows initially and providing the user with controls to load
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
51
Tables
Handling Row Selections
more rows. An even better solution is to display only the most important subset of rows. For example, you
might use location data to limit the number of rows to those that are most relevant to the user’s current
location.
You can use either technique to handle row selections. If your interface controller has more than one table,
using an action method might be preferable because you do not have to identify which table was involved. If
you implement an action method for your table, it must have the following syntax:
- (IBAction)myTableAction:(NSInteger)rowIndex
Use the selection of a table row to perform any relevant actions for your app. For example, you might display
a new interface controller or update the row’s content. If you do not want a table row to be selectable, disable
the Selectable option for the corresponding row controller in your storyboard.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
52
Context Menus
The Retina display with Force Touch found on Apple Watch provides a new way to interact with content.
Instead of just tapping items on the screen, pressing the screen with a small amount of force activates the
context menu (if any) associated with the current interface controller. WatchKit displays the menu over your
content, as shown in Figure 11-1.
Context menus are optional. You use a context menu to display actions related to the current screen and a
context menu can display up to four actions. Each action is represented by a title string and an image. Tapping
an action’s image dismisses the menu and executes the action method associated with that menu item. Tapping
anywhere else dismisses the menu without any further action.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
53
Context Menus
Adding a Context Menu to an Interface Controller
The template images you provide should be smaller than the circular background on which they sit. For more
information about the size of menu images and guidelines for how to create them, see Apple Watch Human
Interface Guidelines .
4. For each item, use the Attributes inspector to specify the menu’s title and image. Both are required.
5. Connect each menu item to an action method in your interface controller class.
Menu action methods have the following format:
- (IBAction)doMenuItemAction
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
54
Context Menus
Handling Taps in a Menu Item
- (IBAction)menuItemAction {
If any state information is required to perform the action, it is your responsibility to store and maintain that
information in your interface controller object. For example, if an action relies on the currently selected row
of a table, your interface controller would include a variable to track the most recently selected row. Also, if
you want to request more information from the user after tapping a menu action, use your action method to
present a modal interface controller.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
55
Glances
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
56
Glance Essentials
A glance is a supplemental way for the user to view important information from your app. Not all apps need
a glance. A glance provides immediately relevant information in a timely manner. For example, the glance for
a calendar app might show information about the user’s next meeting, while the glance for an airline app
might display gate information for an upcoming flight. Figure 12-1 shows the glance for the Lister sample app,
which displays the number of completed items and the number of remaining items on the user’s to-do list.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
57
Glance Essentials
The Glance Life Cycle
Glances are delivered as part of your WatchKit app and WatchKit extension. The interface for your glance resides
in your WatchKit app’s existing storyboard file and that interface is managed by a custom
WKInterfaceController object. However, the only job of your glance interface controller is to set the
contents of the glance. Glances do not support interactivity, and tapping on a glance automatically launches
your WatchKit app.
For information about the life cycle of interface controllers, see WatchKit Extension Life Cycle (page 20).
An app has only one glance interface controller, so that one controller must be able to display the data you
want.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
58
Managing Your Glance Interface
When adding a WatchKit app target to your Xcode project, you can specify whether you want a glance interface.
You can also add a glance to your project later if you forget to add one initially. A glance interface controller
has a slightly different appearance in your app’s storyboard. Specifically, it has a Glance Entry Point object
attached to it and it has a default layout, as shown in Figure 13-1.
Figure 13-1 An interface controller with the glance entry point object
To configure the contents of your glance at runtime, you use a custom WKInterfaceController subclass.
You implement this subclass in the same way that you implement other interface controller classes in your
WatchKit app.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
59
Managing Your Glance Interface
Adding a Glance Interface to Your App
3. Select the glance interface controller in your storyboard and open the Identity inspector.
4. Set the class of your glance interface controller to the class you created in Step 1.
WatchKit apps may have only one glance interface. Do not add more than one glance interface controller to
your app’s storyboard.
To update the content of a glance after it is onscreen, use an NSTimer object to perform periodic updates.
You do not need to update WKInterfaceDate and WKInterfaceTimer objects, which automatically update
themselves.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
60
Managing Your Glance Interface
Customizing App Launch From Your Glance
Calling the updateUserActivity:userInfo: method tells WatchKit to call the main interface controller’s
handleUserActivity: method at launch time. In your implementation of the handleUserActivity:
method, use the provided contextual data to configure your UI appropriately. For example, an app with a
page-based interface might use the provided data to select which page to display initially.
To summarize, to display a different interface controller when launching from your glance, do the following:
● In the glance interface controller:
● Configure the glance normally in the init and willActivate methods.
● Call the updateUserActivity:userInfo: method and use the userInfo parameter to convey
information about the state of the glance to your app.
● In your app’s main interface controller:
● Implement the handleUserActivity: method. Use the provided userInfo dictionary to configure
your UI appropriately.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
61
Notifications
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
62
Notification Essentials
If your iOS app supports local or remote notifications, Apple Watch display those notifications at appropriate
times. When one of your app’s local or remote notification arrives on the user’s iPhone, iOS decides whether
to display that notification on the iPhone or on Apple Watch. For notifications sent to Apple Watch, the system
lets the the user know subtly that a notification is available. If the user chooses to view the notification, the
system displays an abbreviated version of the notification first, followed by a more detailed version. The user
can dismiss the detailed notification, launch your WatchKit app, or act on the notification by tapping an available
action button.
Apps are not required to do anything to support notifications. The system provides a default notification
interface that displays the alert message from the notification. However, apps can customize the notification
interface and include custom graphics, content, and branding.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
63
Notification Essentials
The Short-Look Interface
Note: Apple Watch displays local and remote notifications only if the containing iOS supports them.
For information about how to support local and remote notifications in your iOS app, see Local and
Remote Notification Programming Guide .
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
64
Notification Essentials
The Long-Look Interface
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
65
Notification Essentials
The Long-Look Interface
● The content area contains the detailed information about the incoming notification. For custom interfaces,
you can configure this area to start underneath the sash or just below it. For information on how to
customize the content in this area, see Custom Notification Interfaces (page 69).
● The bottom area contains a Dismiss button and any action buttons registered by the containing iOS app.
Figure 14-2 shows an example of a long-look notification containing two action buttons.
Tapping anywhere in the sash or content area launches your WatchKit app. Tapping one of the app-defined
action buttons delivers the selected action to either your iOS app or your WatchKit app. Foreground actions
are delivered to your WatchKit app and extension while background actions are delivered to your iOS app.
Tapping the Dismiss button closes the notification interface without any further actions.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
66
Notification Essentials
Adding Action Buttons to Notifications
For information about how to provide a custom long-look interface for your app, see Custom Notification
Interfaces (page 69).
Listing 14-1 shows a partial implementation of a method that registers the settings and categories for a sample
iOS app. This method is implemented by the containing iOS app, not the WatchKit extension, and is called by
the iOS app delegate at launch time. The implementation shows the creation and registration of an “invitation”
category that contains actions to accept or decline a meeting invitation.
func registerSettingsAndCategories() {
acceptAction.identifier = "accept"
acceptAction.activationMode = UIUserNotificationActivationMode.Background
acceptAction.authenticationRequired = false
declineAction.identifier = "decline"
declineAction.activationMode = UIUserNotificationActivationMode.Background
declineAction.authenticationRequired = false
inviteCategory.setActions([acceptAction, declineAction],
forContext: UIUserNotificationActionContext.Default)
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
67
Notification Essentials
Responding to Taps in Action Buttons
inviteCategory.identifier = "invitation"
categories.addObject(inviteCategory)
// Configure other actions and categories and add them to the set...
categories: categories)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
For information about how to configure categories and actions in your iOS app, see Local and Remote Notification
Programming Guide .
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
68
Managing a Custom Long Look Interface
The custom long look notification interface consists of two separate interfaces: one static and one dynamic.
The dynamic interface displays a fully customized version of the notification’s content, and can include custom
content and graphics provided by your WatchKit extension. The static interface is a simpler interface that
contains only the notification’s alert message and any static images and text that you configure at design time.
When you add a new notification interface controller to your storyboard file, Xcode creates separate storyboard
scenes for the static and dynamic interfaces, as shown in Figure 15-1. The static scene is required but the
dynamic scene is optional. (In fact, delete the dynamic scene if you can display everything you want using the
static interface and do not need to configure the notification dynamically.) The static and dynamic scenes are
associated with the same notification type, which you configure using the notification category object attached
to the static scene.
When a notification of the correct type arrives, WatchKit chooses your static or dynamic interface based on
several factors. If a dynamic interface is not available, if there is not enough power to warrant displaying the
dynamic interface, or if you explicitly tell WatchKit not to display the dynamic interface, WatchKit displays the
static interface. In all other cases, WatchKit chooses your dynamic interface. After making the choice, WatchKit
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
69
Managing a Custom Long Look Interface
Adding a Custom Notification Interface to Your App
loads the appropriate storyboard resources and prepares the interface as shown in Figure 15-2. The loading
process for the dynamic interface is mostly the same as for your app’s other interface controllers, with the
exception of processing the notification payload, which is specific to notification interface controllers.
3. Select the notification interface controller in your storyboard and open the Identity inspector.
4. Set the class of your notification interface controller to the class you created in Step 1.
Apps may have multiple notification interfaces, which you differentiate using categories. In your storyboard
file, use the Notification Category object to specify the category name associated with each scene. WatchKit
uses the category value to determine which scene to load at runtime. If an incoming notification does not
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
70
Managing a Custom Long Look Interface
Configuring the Category of a Custom Interface
have a category, WatchKit loads the scene whose category name is set to default. For more information
about how to specify the category of a notification interface, see Configuring the Category of a Custom
Interface (page 71).
To assign a notification type to your notification interface, select the Notification Category object in your
storyboard and go to the Attributes inspector, shown in Figure 15-3. Enter the category name in the Name
field of the inspector. You can also set the sash color and title text color for your custom interface in the
inspector.
When generating remote notifications, your server specifies the notification type by including the category
key in the aps dictionary of the payload. For local notifications, you specify this value in the category property
of the UILocalNotification object.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
71
Managing a Custom Long Look Interface
Configuring the Static Notification Interface
Note: The category string also defines which action buttons (if any) are appended to the end of
your notification interface. For more information on supporting custom actions, see Adding Action
Buttons to Notifications (page 67).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
72
Managing a Custom Long Look Interface
Configuring a Dynamic Notification Interface
Figure 15-4 shows the configuration of the static and dynamic scenes for a custom notification interface in a
calendar app. The notification arrow points to the static scene, which contains a custom icon and two labels.
In the static interface, the label containing the string <message> is the one associated with the
notificationAlertLabel outlet and therefore receives the notification’s alert message at runtime.
Figure 15-4 Static and dynamic scenes for a single notification type
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
73
Managing a Custom Long Look Interface
Configuring a Dynamic Notification Interface
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
74
Managing a Custom Long Look Interface
Configuring a Dynamic Notification Interface
- (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification
withCompletion:(void(^)(WKUserNotificationInterfaceType interface)) completionHandler
{
[self.titleLabel setText:titleString];
// Extract the date and time from the custom section of the payload.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
75
Managing a Custom Long Look Interface
Configuring a Dynamic Notification Interface
// Call a custom method to get the localized date format string for the user.
// Call a custom method to get the localized time format string for the user.
[self.dateLabel setText:formattedDateString];
[self.timeLabel setText:formattedTimeString];
[self.notesLabel setText:notesString];
completionHandler(WKUserNotificationInterfaceTypeCustom);
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
76
Managing a Custom Long Look Interface
Testing Your Custom Interface
For more information about setting up the build schemes and configuring your payload data, see The Build,
Run, Debug Process (page 13).
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
77
Document Revision History
Date Notes
2015-01-12 New document that describes how to use the WatchKit framework to
write apps for Apple Watch.
2015-01-12 | Copyright © 2015 Apple Inc. All Rights Reserved. Apple Confidential Information.
78
Apple Inc.
Copyright © 2015 Apple Inc.
All rights reserved.