DEV Community

Steve
Steve

Posted on • Edited on

4 2

How to use WKCrownDelegate in WatchOS Development

Hi everyone.

While working on a WatchOS application recently I needed to perform some operations in the application when the user rotated the digital crown; I had difficulties to find the solution because there were very few resources on the subject, and the official documentation is not clear enough for me. (A full example is always better).
After doing some research on the subject, I finally was able to find a solution and this guide will explain step by step how I achieved it.

So i'm using Swift 5 on XCode 12.4 with WatchKit.

We gonna start from a blank project so it's clear for everybody:

The InterfaceControllercode looks like :

import WatchKit
import Foundation

class InterfaceController: WKInterfaceController {

    override func awake(withContext context: Any?) {
        // Configure interface objects here.
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
    }

}
Enter fullscreen mode Exit fullscreen mode

The first step will be to modify our InterfaceController so it conforms to WKCrownDelegate protocol.
This looks like :

extension InterfaceController : WKCrownDelegate {}
Enter fullscreen mode Exit fullscreen mode

Before implementing our protocol methods, we will need to add some code to our InterfaceController class; we need to tell the crownSequencer it should focus, and define a delete.
To do that, add a didAppear() method (this method is automatically called when the watch interface is visible to user). This looks like :

override func didAppear() {
    super.didAppear()
    crownSequencer.delegate = self
    crownSequencer.focus()
}
Enter fullscreen mode Exit fullscreen mode

Now we are ready to listen to events; the available ones are :

  • func crownDidRotate(WKCrownSequencer?, rotationalDelta: Double)

    Called when the user rotates the crown.

  • func crownDidBecomeIdle(WKCrownSequencer?)

    Called when the user stops rotating the crown.

Full code:

import WatchKit
import Foundation

class InterfaceController: WKInterfaceController {

    override func awake(withContext context: Any?) {
        // Configure interface objects here.
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
    }

    override func didAppear() {
        super.didAppear()
        crownSequencer.delegate = self
        crownSequencer.focus()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
    }

}

extension InterfaceController : WKCrownDelegate {
    func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
        print(rotationalDelta)
    }
}
Enter fullscreen mode Exit fullscreen mode

When the digital crown will rotate, the rotational delta will be printed like on the screenshot below :

Capture d’écran 2021-03-24 à 10.11.51

I hope this article has helped you, feel free to add your suggestions in comments.

Have a nice day! (or night)

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools 🔁

Top comments (1)

Collapse
 
james_christie_e258d6bfa7 profile image
James Christie

Hi Steve,
I am trying to extend what you've done to use the rotationsPerSecond to modify the digitalCrownRotations stride so that the faster a user spins the crown the larger stride taken, I've only just started with swift so this is proving difficult. Any ideas?

developer.apple.com/documentation/...

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay