I'm working with some UIGestures (in particular a force touch). I have all of that working, the additional UI updates/animations all working per the forced touch. However, I would like to add in the haptic touch feedback on the hard press. To my dismay, this snippet of code is not working. The function is called, interface updated, but no haptic feedback. Is there something I'm missing here? Permissions, capability, etc.?
@objc func forceTouchHandler(_ sender: ForceTouchGestureRecognizer) {
print("force touch")
UINotificationFeedbackGenerator().notificationOccurred(.success)
self.updateInterface()
}
Thanks in advance for any feedback.
You don't need any permission to use feedback generator. It will not work when you're using a microphone. But in other cases everything should be fine. Try to call a prepare()
method before notification.
let generator = UINotificationFeedbackGenerator()
generator.prepare()
generator.notificationOccurred(.success)
So, took a bit of digging but appears the issue was that I was testing on a iPhone 6 which does not support the UINotificationFeedbackGenerator
. I figured since they provide the haptic touches on it, and no errors were being thrown all was right in the world.
So, here is what I wound up doing:
let modelName = UIDevice.modelName
if audioModels.contains(modelName) {
AudioServicesPlaySystemSound(1519)
} else {
UINotificationFeedbackGenerator().notificationOccurred(.success)
}
The UIDevice.modelName
is just helper function I found on the internet (prob. Stack) that gets the device name. I then compare that name to a little array I setup - audioModels
- of devices that need to have the audio played instead of using the NotificationFeedbackGenerator
. Not sure if there is a better more intuitive way to check for the functionality per device (let me know if there is) but this is working for me.
Thanks again for looking into the issue.
For me, the problem was haptics were disabled at a system level.
Settings -> Sounds & Haptics -> System Haptics
Ensure this is on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With