I'm building my first watchOS app, and I'm new to Swift but loving it so far.
I'm trying to create a button that, when pressed, starts listening in dictation mode. I've seen this done in other watchOS apps (e.g. Drafts).
Is this possible in SwiftUI alone (e.g. via some modifier of TextField), or do I need to bridge into UIKit?
If only the latter is possible, I've tried this approach but clicking the button doesn't seem to do anything. What am I missing?
import SwiftUI
struct ContentView: View {
var body: some View {
Button(action: {
presentInputController(withSuggestions: ["testing"], completion: {(answer) -> Void in })
}) {
Image(systemName: "plus")
.font(.largeTitle)
}
}
}
extension View {
typealias StringCompletion = (String) -> Void
func presentInputController(withSuggestions suggestions: [String], completion: @escaping StringCompletion) {
WKExtension.shared()
.visibleInterfaceController?
.presentTextInputController(withSuggestions: suggestions,
allowedInputMode: .plain) { result in
guard let result = result as? [String], let firstElement = result.first else {
completion("")
return
}
completion(firstElement)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}