UIImpactFeedbackGenerator not working
Asked Answered
M

7

9

I'm trying to use the new UIImpactFeedbackGenerator for haptic feedback, but it isn't working.

  • Testing on iPhone 7
  • iOS 10.1.1
  • System Haptic setting is enabled

I'm using the following code example in Objective-C

UIImpactFeedbackGenerator *myGen = [[UIImpactFeedbackGenerator alloc] init];
[myGen initWithStyle:(UIImpactFeedbackStyleMedium)];
[myGen impactOccurred];
myGen = NULL;

I'm triggering it inside a UILongPressGestureRecognizer delegate.

Any idea what the problem might be?

Markswoman answered 25/11, 2016 at 9:19 Comment(3)
It is recommended to call prepare before calling impactOccurred, have you tried that?Kubetz
@Kubetz yeah, i tried that as well, didn't help :/Markswoman
Have you tried not releasing myGen it in the same method ? Also, try moving the declaration of your generator somewhere earlier in the lifecycle and make sure you prepare it before firing. Does this help ?Uninterrupted
H
13

I had the same issue, it turned out that while working with real time camera input, UIImpactFeedbackGenerator will not give any haptic feedback

Homeopathist answered 24/10, 2019 at 15:6 Comment(1)
sooo.. whats the approach here ? if we have a Camera App we cant use this ?Embowel
F
8

I ran into the same problem, from the documentation:

...Note that calling these methods does not play haptics directly. Instead, it informs the system of the event. The system then determines whether to play the haptics based on the device, the application’s state, the amount of battery power remaining, and other factors. For example, haptic feedback is currently played only:

  • On a device with a supported Taptic Engine (iPhone 7 and iPhone 7 Plus).
  • When the app is running in the foreground. When the System
  • Haptics setting is enabled.

I guess you just have to trust the system to known when it should fire... I didn't like this approach for obvious reasons -so used AudioServicesPlaySystemSound and k​System​Sound​ID_Vibrate:

...On some iOS devices, you can pass the k​System​Sound​ID_Vibrate constant to invoke vibration. On other iOS devices, calling this function with that constant does nothing.

Objective-C

import AudioToolbox
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

Swift 3

import AudioToolbox
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
Fifteenth answered 10/4, 2017 at 3:43 Comment(0)
I
5

Probably you should not use double init.

UIImpactFeedbackGenerator *myGen = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleMedium];
[myGen impactOccurred];

Also please make sure that you did [[AVAudioSession sharedInstance] setAllowHapticsAndSystemSoundsDuringRecording:YES]; before this call.

Internist answered 9/10, 2021 at 14:13 Comment(0)
D
1

I asked an Apple Engineer at WWDC 2017 why my haptic request would sometimes fail, and he had me call impactOccurred on the next run loop like this:

[self.feedbackGenerator performSelector:@selector(impactOccurred) withObject:nil afterDelay:0.0f];

Worked like a charm!

Dituri answered 27/7, 2017 at 3:26 Comment(1)
Still not working for me. I noticed that if I print the feeback object, it says it is not prepared, even when I do call prepare as well.Entablature
A
0

In my case - the setting in iphone was disabled.

Settings -> Sounds and Haptics -> System Haptics

After enabling it - haptics become work.

enter image description here

Alarick answered 31/7, 2023 at 8:53 Comment(0)
I
0

How to use Haptic Feedback for a better experience in iOS

UIImpactFeedbackGenerator
private let feedbackGenerator = UIImpactFeedbackGenerator(style: .light)

//The Impact Feedback style has three variations 
// .light  .medium  .heavy


feedbackGenerator.impactOccurred()
Inexplicit answered 21/2, 2024 at 8:26 Comment(0)
A
0

Check also that iPhone Settings -> Vibrations is Enabled.

enter image description here

enter image description here

Atony answered 9/4, 2024 at 10:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.