WKInterfaceView

The WKInterfaceView struct helps you to use WatchKiit WKInterfaceObject or its derived class in project using SwiftUI.WKInterfaceView is a type that represents part of your app’s user interface using WatchKit and provides modifiers that you use to configure views.

You create custom views by declaring types that conform to the View protocol. Implement the required View/body-swift.property computed property to provide the content for your custom view. Then you can present your WatchKit WKInterfaceObject by using WKInterfaceView(MyWKInterfaceObject()) , as follows.

struct MyWKInterfaceObject: View {
    var body: some View {
        WKInterfaceView(MyWKInterfaceObject())
    }
}

The View protocol provides a large set of modifiers, defined as protocol methods with default implementations, that you use to position and configure views in the layout of your app. Modifiers typically work by wrapping the view instance on which you call them in another view with the specified characteristics. For example, adding the View/opacity(_:) modifier to a interface view returns a new view with some amount of transparency:

WKInterfaceView(MyWKInterfaceObject())
    .opacity(0.5) // Display partially transparent interface view.

It is recommended to use ZStack with WKInterfaceView , as follows.

ZStack {
    WKInterfaceView(MyWKInterfaceObject())
    MySwiftUIView()
}

A view that represents a WatchKit interface object.

Use a WKInterfaceObjectRepresentable instance to create and manage a doc://com.apple.documentation/documentation/WatchKit/WKInterfaceObject in your SwiftUI interface. Adopt this protocol in one of your app’s custom instances, and use its methods to create, update, and tear down your interface object. The creation and update processes parallel the behavior of SwiftUI views, and you use them to configure your interface object with your app’s current state information. Use the teardown process to remove your interface object cleanly from your SwiftUI. For example, you might use the teardown process to notify other parts of your app that the interface object is disappearing.

To add your interface object into your SwiftUI interface, create your UIViewRepresentable instance and add it to your SwiftUI interface. The system calls the methods of your representable instance at appropriate times to create and update the interface object.

The system doesn’t automatically communicate changes occurring within your interface object to other parts of your SwiftUI interface. When you want your interface object to coordinate with other SwiftUI views, you must provide a NSViewControllerRepresentable/Coordinator instance to facilitate those interactions. For example, you use a coordinator to forward target-action and delegate messages from your interface object to any SwiftUI views.

  • The type of view to present.

  • A closure helps you finish some special things like work to be done in WatchKit interface.

Initialization

  • Creates an WKInterfaceView instance from the specified parameters.

  • Creates a WatchKit interface object and configures its initial state.

    You must implement this method and use it to create your interface object. Configure the object using your app’s current data and contents of the context parameter. The system calls this method only once, when it creates your interface object for the first time. For all subsequent updates, the system calls the WKInterfaceObjectRepresentable/updateWKInterfaceObject(_:context:) method.

  • Updates the presented WatchKit interface object (and its coordinator) to the latest configuration.

    When the state of your app changes, SwiftUI updates the portions of your interface affected by those changes. SwiftUI calls this method for any changes affecting the corresponding interface object. Use this method to update the configuration of your object to match the new state information provided in the context parameter.

  • A return type for function makeCoordinator.

  • Creates the custom instance that you use to communicate changes from your interface object to other parts of your SwiftUI interface.

    Implement this method if changes to your interface object might affect other parts of your app. In your implementation, create a custom Swift instance that can communicate with other parts of your interface. For example, you might provide an instance that binds its variables to SwiftUI properties, causing the two to remain synchronized. If your interface object doesn’t interact with other parts of your app, providing a coordinator is unnecessary.

    SwiftUI calls this method before calling the WKInterfaceObjectRepresentable/makeWKInterfaceObject(context:) method. The system provides your coordinator either directly or as part of a context structure when calling the other methods of your representable instance.