Reputation: 140
I'm trying to print crown input in Apple Watch and I can't get it to work.
I have created a new watch app (for watch only).
I have this in my HostingController:
import WatchKit
import Foundation
import SwiftUI
class HostingController: WKHostingController<ContentView>, WKCrownDelegate {
override func awake(withContext context: Any?) {
super.awake(withContext: context)
print("awake")
crownSequencer.delegate = self
}
override func willActivate() {
super.willActivate()
print("willActivate")
crownSequencer.focus()
}
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
print("\(rotationalDelta)")
}
override var body: ContentView {
return ContentView()
}
}
In the console "awake" and "willActivate" are both showing up. But when I am rotating the crown nothing shows up.
What am I missing?
Upvotes: 3
Views: 648
Reputation: 16467
As you are using WKHostingController
, you will need to use the swiftUI API's for the digital crown. It appears the standard (old) API just fails silently.
You will need a @State
variable to bind the crown to, to make ui element focusable and to add the crown modifier.
@State var rotationValue = 0.0
var body: some View {
Text("Crown did rotate: \(rotationValue)")
.focusable(true)
.digitalCrownRotation($rotationValue)
}
Upvotes: 4
Reputation: 11539
Seriously, if you use a swiftUI solution, you are supposed to use combine
to perform the communication, i.e. monitoring the crownSequencer value.
Upvotes: 0
Reputation: 140
OK after a lot of digging and testing I solved this.
The crown will register movements in WKInterfaceController class extension and not in WKHostingController that I was referring to in my question.
I am very new to watch development so I can't figure out why is this happening, but it works.
Upvotes: 0