Reputation: 4444
I'm trying to use the new UIImpactFeedbackGenerator
for haptic feedback, but it isn't working.
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?
Upvotes: 9
Views: 9305
Reputation: 349
private let feedbackGenerator = UIImpactFeedbackGenerator(style: .light)
//The Impact Feedback style has three variations
// .light .medium .heavy
feedbackGenerator.impactOccurred()
Upvotes: 0
Reputation: 13761
In my case - the setting in iphone was disabled.
Settings -> Sounds and Haptics -> System Haptics
After enabling it - haptics become work.
Upvotes: 1
Reputation: 73
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.
Upvotes: 5
Reputation: 131
I had the same issue, it turned out that while working with real time camera input, UIImpactFeedbackGenerator will not give any haptic feedback
Upvotes: 13
Reputation: 1538
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!
Upvotes: 1
Reputation: 2329
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 kSystemSoundID_Vibrate:
...On some iOS devices, you can pass the kSystemSoundID_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))
Upvotes: 8