Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Haptic Feedback not firing

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.

like image 466
djneely Avatar asked Sep 21 '18 19:09

djneely


3 Answers

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)
like image 158
Yury Imashev Avatar answered Oct 10 '22 20:10

Yury Imashev


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.

like image 28
djneely Avatar answered Oct 10 '22 20:10

djneely


For me, the problem was haptics were disabled at a system level.

Settings -> Sounds & Haptics -> System Haptics

Ensure this is on.

like image 5
Ric Santos Avatar answered Oct 10 '22 19:10

Ric Santos