Contents

WKInterfaceController

A class that provides the infrastructure for managing the interface in a watchOS app.

Declaration

@MainActor class WKInterfaceController

Overview

An interface controller serves the same purpose as a UIViewController object in a UIKit app, except that it doesn’t 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 WKInterfaceController 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.

Initialize 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 notification and app interfaces; WatchKit uses the information in your app’s main storyboard file to determine which interface controller to load. 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 interface controller at launch time.

When creating an interface controller, WatchKit instantiates the class and calls its init() method. You can use this method to initialize variables and load data; however, don’t use it to configure your user interface. The controller’s user interface elements may not be properly initialized when this method runs.

Next, the system calls the awake(withContext:) method. If WatchKit passes a valid object to the awake(withContext:) method, use the information in that object to customize the initialization process. Also, the controller’s user interface elements are guaranteed to be available at this point. This means that you can safely use this method to configure your user interface.

The willActivate() method lets you know when your interface is about to become active. Use the willActivate() method to perform any last minute tasks, such as checking for updates to your content; however, don’t use it for your primary initialization.

The willActivate() method may be called at times when your interface isn’t yet onscreen. For example, WatchKit may call the method in advance so that you have time to update your content. WatchKit calls the didAppear() method to let you know when your interface becomes visible. Similarly, WatchKit calls the willDisappear() and didDeactivate() methods when your interface moves offscreen again.

In iOS Simulator, WatchKit calls the didDeactivate() 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 willActivate() method again. You can use this capability to debug your activation and deactivation code.

Interface Builder configuration options

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

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 Settitle(_:) 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 doesn’t apply to glance or notification interface controllers.

Activity Indicator On Load

A Boolean value that indicates whether the interface controller’s contents are hidden until the Willactivate() method returns. When you enable this option, the system displays a progress indicator until the Willactivate() 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 that turns off scrolling and allows built-in controls and containers to fill content to the screen edges, regardless of the content-safe area.

Full Screen

A Boolean value that determines whether SpriteKit or SceneKit content can use the full screen. The system hides the status bar but displays the time in the upper-right corner with a gradient behind it, making the time clearly visible against the scene.

Fixed to screen edges

A Boolean value that indicates whether the contents ignore the safe area and minimum layout margins. When you enable this option, the system turns off scrolling, and allows built-in controls and containers to fill content to the screen edges.

Background

The background image displayed behind the scene’s content. The image specified 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 Contentmode Swift.enum 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. Select Custom to specify different values for the top, bottom, left, and right edges.

Spacing

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

Subclassing notes

Subclass WKInterfaceController 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 can’t 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 WKUserNotificationInterfaceController 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(withContext:) methods. Override any other methods that make sense based on your needs.

Topics

Creating the interface controller

Responding to activation and appearance events

Implementing a navigation interface

Presenting interface controllers modally

Navigating a page-based interface

Managing segue-based transitions

Managing Scrolling

Respecting safe areas and layout margins

Animating changes to the interface

Handling text input

Presenting video and audio interfaces

Handling table-row selections

Managing pickers

Getting the crown sequencer

Coordinating Handoff activity

Adding PassKit passes

Managing Notifications

Deprecated symbols

See Also

User interface basics