Class

WKInterface​Controller

The main class for implementing the interface in a watchOS app.

Overview

An interface controller serves the same purpose as a UIView​Controller object in a UIKit app, except that it does not manage any actual views. It runs in your WatchKit extension and remotely manages the behavior associated with an interface controller in your Watch app’s storyboard file. You subclass WKInterface​Controller and use its methods to configure the elements of your storyboard scene and to respond to interactions with those elements.

Your interface controller code runs locally on the user’s Apple Watch but is separate from the interface that it manages. When you change the value of an interface object in your code, the system forwards the needed information to your Watch app, which makes the corresponding changes onscreen.

Initializing Your Interface Controllers

When the user interacts with your app content, the system launches your extension and creates the appropriate interface controller objects automatically. Apps use different interface controllers to manage their glance, notification, and app interfaces. WatchKit uses the information in your app’s main storyboard file to determine which interface controller to load. Glance and notification scenes are configured specially so that the system can identify them. For your app, WatchKit loads your app’s main interface controller initially, but you may change the initial initial interface controller at launch time.

When creating an interface controller, WatchKit instantiates the class and calls its init() method followed shortly by its awake(with​Context:​) method. Use those methods to initialize variables, load data, and configure the items in your storyboard scene. If WatchKit passes a valid object to the awake(with​Context:​) method, use the information in that object to customize the initialization process.

The will​Activate() method lets you know when your interface is active. Use the will​Activate() method to perform any last minute tasks, such as checking for updates to your content. (Do not use it for your primarily initialization.) The willActivate method may be called at times when your interface is not yet onscreen. For example, WatchKit may call the method in advance so that you have time to update your content. WatchKit calls the did​Appear() method to let you know when your interface becomes visible. Similarly, WatchKit calls the will​Disappear() and did​Deactivate() methods when your interface moves offscreen again.

In iOS Simulator, WatchKit calls the did​Deactivate() method for the current interface controller when you lock the simulator by selecting Hardware > Lock. When you subsequently unlock the simulator, WatchKit calls that interface controller’s will​Activate() method again. You can use this capability to debug your activation and deactivation code.

Displaying Your App’s Interface Controllers

Your Watch app may use one of two navigation styles for displaying content. The hierarchical navigation style lets you push and pop interface controllers similar to a navigation controller on iOS. The page-based style lets the user navigate between two or more interface controllers by swiping horizontally.

For page-based navigation, you normally specify the interface controllers for each page at design time. Once configured, you do not need to make any calls to facilitate navigation from page to page. WatchKit automatically displays the next page in the sequence when the user swipes horizontally. If you want to change the currently displayed pages, you can do so at any time by calling the reload​Root​Controllers(with​Names:​contexts:​) class method. Typically, you call that method early in your app’s launch cycle but you may call it at any time to reload your interface.

For hierarchical interfaces, you are responsible for pushing new interface controllers onscreen in response to user actions. When you want to display new content, call the push​Controller(with​Name:​context:​) method and specify the name of the interface controller you want to display. WatchKit handles all of the animations associated with displaying the new interface controller.

When pushing a new interface controller, it is recommended that you pass a context object to the push​Controller(with​Name:​context:​) method. A context object is the main way to communicate information to the new interface controller. For example, you might use this object to specify the data that you want the new interface controller to display. A context object can be an existing data object, or it can be a dictionary that you create dynamically and fill with relevant information.

When using segues to initiate hierarchical navigation between interface controllers, WatchKit calls the context​For​Segue(with​Identifier:​in:​row​Index:​) or context​For​Segue(with​Identifier:​) method based on whether the source of the segue was a table row or a button. Use those methods to provide the context object needed to initialize the new interface controller.

Presenting an Interface Controller Modally

Apps can present an interface controller modally to gather or display information. A modally presented interface controller animates up from the bottom of the screen and covers the current interface. Dismissing the interface reverses the animation and reveals the previously covered interface controller again. When presenting a modal interface, you can display a single interface controller (which is the most common use) or you can display multiple interface controllers in a page-based layout.

Use the present​Controller(with​Name:​context:​) and present​Controller(with​Names:​contexts:​) methods to present your interface controllers modally. If the modal interface controller does not have a title, its title is set to the string Cancel by default. You can set the value of this string in your storyboard or change it at runtime using the set​Title(_:​) method. When the user taps the title string, WatchKit automatically dismisses the modal interface without taking any further actions.

Presenting Standard System Interfaces

The system supports several standard interfaces for gathering input or displaying specific types of information.

The standard interfaces have built-in buttons so that the user can dismiss them at any time. Many of the interfaces also have a dismiss method that you can use to close the interface programmatically. While an interface is active, your app does not have direct control over the interactions with that interface. Use actions or a completion handler to handle any tasks related to the interface itself.

Interface Builder Configuration Options

Xcode lets you configure information about your interface controller in your storyboard file. Table 1 lists the attributes you can configure in your storyboard and their meaning.

Table 1

Interface controller attributes

Attribute

Description

Identifier

The name of the interface controller. Use this name to specify which interface controller to push or present.

Title

The title string assigned to the interface controller. You can set this value programmatically using the set​Title(_:​) method.

Is Initial Controller

A Boolean indicating whether the object is the app’s root interface controller. Only one interface controller at a time may have this option enabled. This option does not apply to glance or notification interface controllers.

Display Activity Indicator When Loading

A Boolean indicating whether the interface controller’s contents should be hidden until after the will​Activate() method returns. When this option is enabled, the system displays a progress indicator until the will​Activate() method returns. You might disable this option if your interface contains mostly static information that can be displayed right away.

Always Bounce

A Boolean value indicating whether scrolling should always cause the screen to bounce, regardless of the length of your content. For content that does not require scrolling, you can enable this option to cause the bounce effect and show that your content does not need to scroll.

Background

The background image to be displayed behind the scene’s content. The image you specify in your storyboard scrolls with your interface controller’s content.

Mode

The content mode for the background image. This mode defines how the background image scales or fills the screen and behaves in the same way as the constants for the UIView​Content​Mode type.

Animate

A Boolean value indicating whether an animated background image starts running its animation automatically after being loaded. Set this option to Yes if you want the animation to start automatically; set it to No if you prefer to start the animation programmatically.

Color

The background color to be displayed behind the scene’s content.

Insets

The amount of space (in points) to insert between the edges of the interface controller and its content. Selecting Custom lets you specify different values for the top, bottom, left, and right edges.

Spacing

Additional spacing (in points) to include between items in the interface controller.

Table 2 lists the attributes you can configure for a glance interface controller.

Table 2

Glance interface controller attributes

Attribute

Description

Identifier

The name of the glance interface controller.

Display Activity Indicator When Loading

A Boolean value indicating whether the system displays a loading indicator for the glance. Glances display a snapshot of their pervious content until the will​Activate() method returns. When this option is enabled, the system also displays a loading indicator and information about when the glance was last updated while the snapshot is visible. When this option is disabled, the system does not display those visual indicators.

Upper

The template to use in the upper portion of the glance. Select the template that best suits the information you want to display.

Lower

The template to use in the lower portion of the glance. Select the template that best suits the information you want to display.

Subclassing Notes

Subclass WKInterface​Controller when you have a storyboard scene that requires configuration at runtime or that handles user interactions. Typically, you define a custom subclass for each unique storyboard scene that your app manages. In your subclass, define outlets for any interface objects you need to configure and define action methods for responding to interactions with the elements of your storyboard scene.

Most custom interface controllers you use in your app require a custom interface controller subclass. Even glances need an interface controller to update the glance contents. The only storyboard scene that cannot use a custom interface controller is the scene associated with a static notification interface. When implementing an interface controller for your dynamic notification interface, subclass WKNotificationInterfaceController instead.

Override any methods of the class needed to configure your interface and get it ready to display. Most interface controllers override the init() and awake(with​Context:​) methods. Override any other methods that make sense based on your needs.

Symbols

Initializing the Interface Controller

init()

Returns an initialized interface controller object.

func awake(with​Context:​ Any?)

Initializes the interface controller with the specified context data.

Responding to Activation and Appearance Events

func will​Activate()

Called to let you know that the interface controller’s is active.

func did​Deactivate()

Called to let you know that the interface controller is no longer active.

func did​Appear()

Called to let you know that the interface controller’s content is now onscreen.

func will​Disappear()

Called to let you know that the interface controller’s content is now offscreen.

Implementing a Navigation Interface

func push​Controller(with​Name:​ String, context:​ Any?)

Pushes a new interface controller onto the screen.

func pop()

Pops the current interface controller from the screen.

func pop​To​Root​Controller()

Pops all interface controllers except the app’s initial interface controller.

Presenting Interface Controllers Modally

func present​Controller(with​Name:​ String, context:​ Any?)

Presents a single interface controller modally.

func dismiss()

Dismisses the current interface controller from the screen.

Navigating a Page-Based Interface

class func reload​Root​Controllers(with​Names:​ [String], contexts:​ [Any]?)

Loads the specified interface controllers and rebuilds the app’s page-based interface.

func become​Current​Page()

Displays the interface controller in the page-based interface.

Managing Segue-Based Transitions

func context​For​Segue(with​Identifier:​ String)

Returns the context object to pass to the specified interface controller when a button is tapped.

func contexts​For​Segue(with​Identifier:​ String)

Returns the context objects to pass to a page-based set of interface controllers when a button is tapped.

func context​For​Segue(with​Identifier:​ String, in:​ WKInterface​Table, row​Index:​ Int)

Returns the context object to pass to the specified interface controller when a row in a table is tapped.

func contexts​For​Segue(with​Identifier:​ String, in:​ WKInterface​Table, row​Index:​ Int)

Returns the context objects to pass to a page-based set of interface controllers when a row in a table is tapped.

Animating Changes to the Interface

func animate(with​Duration:​ Time​Interval, animations:​ () -> Void)

Animates changes to one or more interface objects over the specified duration.

Handling Table-Row Selections

func table(WKInterface​Table, did​Select​Row​At:​ Int)

Called to let you know that the user selected a row in the table.

Managing Pickers

func picker​Did​Focus(WKInterface​Picker)

Called to let you know that the specified picker is now receiving input from the Digital Crown.

func picker​Did​Resign​Focus(WKInterface​Picker)

Called to let you know that the specified picker is no longer receiving input from the Digital Crown.

func picker​Did​Settle(WKInterface​Picker)

Called to let you know when the user settles on a value in a picker.

Getting the Crown Sequencer

var crown​Sequencer:​ WKCrown​Sequencer

The object to use when directly tracking crown events.

Configuring the Context Menu

func add​Menu​Item(with:​ WKMenu​Item​Icon, title:​ String, action:​ Selector)

Adds an action to the context menu using a system-provided icon.

func add​Menu​Item(with​Image​Named:​ String, title:​ String, action:​ Selector)

Adds an action to the context menu using an existing image resource in your Watch app bundle.

func add​Menu​Item(with:​ UIImage, title:​ String, action:​ Selector)

Adds an action to the context menu by using an image provided by your WatchKit extension.

func clear​All​Menu​Items()

Removes all programmatically added actions from the context menu.

Setting the Display Title

func set​Title(String?)

Sets the title of the interface.

Coordinating Handoff Activity

func invalidate​User​Activity()

Invalidates the most recent user activity.

func handle​User​Activity([Any​Hashable :​ Any]?)

Responds to Handoff–related activity.

Adding PassKit Passes

func dismiss​Add​Passes​Controller()

Dismisses the pass interface controller

Getting the Interface Dimensions

var content​Frame:​ CGRect

The frame rectangle used to display your app’s content.

Managing Glance Updates

func begin​Glance​Updates()

Tells the system that you are about to start a potentially length update task for your glance.

func end​Glance​Updates()

Tells the system that you finished updating your glance content.

Responding to Notifications

func handle​Action(with​Identifier:​ String?, for:​ UNNotification)

Delivers a notification payload and a user-selected action to the interface controller.

func handle​Action(with​Identifier:​ String?, for​Remote​Notification:​ [Any​Hashable :​ Any])

Delivers a remote notification payload and a user-selected action to the interface controller.

Deprecated
func handle​Action(with​Identifier:​ String?, for:​ UILocal​Notification)

Delivers a local notification payload and a user-selected action to the interface controller.

Deprecated

Constants

WKMenu​Item​Icon

Template images that you can use for menus.

WKText​Input​Mode

The input modes supported by the text input controller.

Text Response Key

Keys for retrieving text response information.

WKAlert​Controller​Style

Constants indicating the styles for standard system alerts.

Media Player Options

Keys indicating media playback options.

WKVideo​Gravity

Constants indicating the appearance of video content.

WKAudio​Recorder​Preset

Constants indicating the quality of audio recordings.

Audio Recording Options

Options to specify when recording audio.

Error Domain

The domain for WatchKit errors.

Watch​Kit​Error.Code

Error codes reported by WatchKit.

Notifications

let WKAccessibility​Voice​Over​Status​Changed:​ String

Posted by WatchKit when VoiceOver starts or stops. This notification does not include a parameter.

Relationships

Inherits From