I have created a custom keyboard using SwiftUI. This is in the app itself, not a keyboard extension, as I don't want it to be part of the system. I would like the buttons of this keyboard to have the audio feedback as with the system keyboard. The way to achieve this would be to implement the UIInputViewAudioFeedback
into your view and set the enableInputClicksWhenVisible
to true. This allows UIDevice.current.playInputClick()
to play the 'click' sound.
However, the UIInputViewAudioFeedback
inherits from NSObjectProtocol
, which means I cannot add this protocol to a ButtonStyle
or any other SwiftUI.View
. How would I go about adding the system click sounds to this button?
struct KeyboardButtonStyle: SwiftUI.ButtonStyle {
public func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
.font(.title)
.accessibilityAddTraits(.isKeyboardKey)
.onTapGesture {
UIDevice.current.playInputClick()
}
}
}
and then in the body of my keyboard view (example code)
var body: some View {
Button(action: {
// Keyboard button action
}, label: {
Text("Click")
}.buttonStyle(KeyboardButtonStyle())
}