'Keyboard click button press SwiftUI
I know how to enable the keyboard click sounds when using UIKit, but I can't seem to figure out how to enable the system click sound using SwiftUI. I'm currently trying to add it to a ButtonStyle
import AVFoundation
import SwiftUI
struct KeyboardButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.onTapGesture {
UIDevice.current.playInputClick()
}
}
}
I then attempted to make the button style conform to UIInputViewAudioFeedback, so I could add the enableInputClicksWhenVisible property, but that gives me two errors
struct KeyboardButtonStyle: ButtonStyle, UIInputViewAudioFeedback {
// Non-class type 'KeyboardButtonStyle' cannot conform to class protocol 'NSObjectProtocol'
// Non-class type 'KeyboardButtonStyle' cannot conform to class protocol 'UIInputViewAudioFeedback'
}
As a last resort I could use AudioServicesPlaySystemSound(1104), which appears to be some kind of keyboard sound, as well as 1105, but that is not a very clean solution.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
