Class

WKInterfaceController

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

Declaration

@interface WKInterfaceController : NSObject

Overview

An interface controller serves the same purpose as a UIViewController 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 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 awakeWithContext: method. If WatchKit passes a valid object to the awakeWithContext: 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 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 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. 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 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 does not apply to glance or notification interface controllers.

Activity Indicator On Load

A Boolean indicating whether the interface controller’s contents should be hidden until the willActivate method returns. When this option is enabled, 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 content should ignore the safe area and minimum layout margins. When this option is enabled, 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 UIViewContentMode 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.

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 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 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 awakeWithContext: methods. Override any other methods that make sense based on your needs.

Topics

Initializing the Interface Controller

- init

Returns an initialized interface controller object.

- awakeWithContext:

Initializes the interface controller with the specified context data.

Responding to Activation and Appearance Events

- willActivate

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

- didDeactivate

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

- didAppear

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

- willDisappear

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

Implementing a Navigation Interface

- pushControllerWithName:context:

Pushes a new interface controller onto the screen.

- popController

Pops the current interface controller from the screen.

- popToRootController

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

Presenting Interface Controllers Modally

- presentControllerWithName:context:

Presents a single interface controller modally.

- presentControllerWithNames:contexts:

Presents a page-based interface modally.

- presentAlertControllerWithTitle:message:preferredStyle:actions:

Presents an alert or action sheet over the current interface controller.

WKAlertControllerStyle

Constants indicating the styles for standard system alerts.

- dismissController

Dismisses the current interface controller from the screen.

Navigating a Page-Based Interface

+ reloadRootPageControllersWithNames:contexts:orientation:pageIndex:

Loads the specified interface controllers and rebuilds the app’s page-based interface for the given scrolling orientation.

WKPageOrientation

Scrolling orientations for page-based interfaces.

- becomeCurrentPage

Displays the interface controller in the page-based interface.

Managing Segue-Based Transitions

- contextForSegueWithIdentifier:

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

- contextsForSegueWithIdentifier:

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

- contextForSegueWithIdentifier:inTable:rowIndex:

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

- contextsForSegueWithIdentifier:inTable:rowIndex:

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

Scrolling

- scrollToObject:atScrollPosition:animated:

Scrolls the specified object to the given position onscreen.

WKInterfaceScrollPosition

Onscreen scroll positions.

- interfaceDidScrollToTop

Tells the interface controller that the user has performed a scroll-to-top gesture (for example, tapping the status bar) and that the scrolling animation has finished.

- interfaceOffsetDidScrollToTop

Tells the interface controller that the user has scrolled to the top of the interface and that the scrolling animation has finished.

- interfaceOffsetDidScrollToBottom

Tells the interface controller that the user has scrolled to the bottom of the interface and that the scrolling animation has finished.

tableScrollingHapticFeedbackEnabled

A Boolean value that determines whether haptic feedback coordinates with the appearance of new rows as the user scrolls through a table.

Respecting Safe Areas and Layout Margins

contentSafeAreaInsets

Insets that define the area in which it’s safe to display content on the screen.

systemMinimumLayoutMargins

Leading and trailing insets that represent the minimum layout margins for text elements.

Animating Changes to the Interface

- animateWithDuration:animations:

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

Handling Text Input

- presentTextInputControllerWithSuggestions:allowedInputMode:completion:

Displays a modal interface for gathering text input from the user.

- presentTextInputControllerWithSuggestionsForLanguage:allowedInputMode:completion:

Displays a modal interface for gathering language-specific text input from the user.

- dismissTextInputController

Dismisses the text input controller without returning any text.

WKTextInputMode

The input modes supported by the text input controller.

Presenting Video and Audio Interfaces

- presentMediaPlayerControllerWithURL:options:completion:

Displays a modal interface for playing the specified media file.

Media Player Options

Keys indicating media playback options.

- dismissMediaPlayerController

Dismisses the media interface controller.

- presentAudioRecorderControllerWithOutputURL:preset:options:completion:

Display a standard interface for recording audio from the user’s Apple Watch.

WKAudioRecorderPreset

Constants indicating the quality of audio recordings.

Audio Recording Options

Options to specify when recording audio.

- dismissAudioRecorderController

Dismisses the audio recording interface controller.

Handling Table-Row Selections

- table:didSelectRowAtIndex:

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

Managing Pickers

- pickerDidFocus:

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

- pickerDidResignFocus:

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

- pickerDidSettle:

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

Getting the Crown Sequencer

crownSequencer

The object to use when directly tracking crown events.

Configuring the Context Menu

- addMenuItemWithItemIcon:title:action:

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

- addMenuItemWithImageNamed:title:action:

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

- addMenuItemWithImage:title:action:

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

- clearAllMenuItems

Removes all programmatically added actions from the context menu.

WKMenuItemIcon

Template images that you can use for menus.

Setting the Display Title

- setTitle:

Sets the title of the interface.

Coordinating Handoff Activity

- updateUserActivity:

Registers the current user activity with the system.

- invalidateUserActivity

Invalidates the most recent user activity.

Adding PassKit Passes

- presentAddPassesControllerWithPasses:completion:

Displays a modal interface for presenting passes to the user.

- dismissAddPassesController

Dismisses the pass interface controller

Getting the Interface Dimensions

contentFrame

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

Managing Glance Updates

- beginGlanceUpdates

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

Deprecated
- endGlanceUpdates

Tells the system that you finished updating your glance content.

Deprecated

Notifications

WKAccessibilityVoiceOverStatusChanged

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

WKAccessibilityReduceMotionStatusDidChangeNotification

Posted by WatchKit when reduced motion is enabled or disabled. This notification does not include a parameter.

Deprecated Symbols

- handleUserActivity:

Responds to Handoff–related activity.

Deprecated
- updateUserActivity:userInfo:webpageURL:

Registers the current user activity with the system

Deprecated
+ reloadRootControllersWithNames:contexts:

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

Deprecated
Text Response Key

Keys for retrieving text response information.

Relationships

Inherits From

See Also

User Interface Basics

Building watchOS app Interfaces Using the Storyboard

Create the user interface for your watchOS app by nesting stacks.

WKInterfaceObject

An object that provides information that is common to all interface objects in your watchOS app.

WKAlertAction

An object that encapsulates information about a button displayed in an alert or action sheet.

WKAccessibilityImageRegion

An object that defines a portion of an image that you want to call out separately to an assistive app.

WKAccessibilityIsVoiceOverRunning

Returns a Boolean value indicating whether VoiceOver is running.

WKAccessibilityIsReduceMotionEnabled

Returns a Boolean value indicating whether reduced motion is enabled.