How to check if Haptic Engine (UIFeedbackGenerator) is supported
Based on Apple's UIFeedbackGenerator
documentation, sounds like iOS does that for you.
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.
Even if you don't need to worry about check whether the device can do haptic feedback, you still need to ensure it's called only with iOS 10 or greater, so you could accomplish that with this:
if #available(iOS 10,*) {
// your haptic feedback method
}
Here's a quick summary of the various haptic feedback options available in iOS 10.
iOS Haptic Feedback not firing
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.
Haptic engine iPhone stronger impact
func generateVibration() {
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.impactOccurred()
generator.impactOccurred()
}
The more times you call generator.impactOccured(), the stronger the impact will be.
Related Topics
Change the Alpha Value of the Navigation Bar
Peripheral and Central at the Same Time on iOS
Clipstobounds Causes Uiimage to Not Display in iOS10 & Xcode 8
Uilabel: Background Dependent Color
Ios: How to Read an Audio File into a Float Buffer
Share Image with Hashtag via Uiactivityviewcontroller (Twitter, Facebook, Instagram)
Swift: Return Array of Type Self
How to Set Selected Segment Index in Uisegmentedcontrol
Will Apps That Use Telprompt Be Rejected
Uitableviewcell Selected Background Color on Multiple Selection
How to Dynamically Format a Number to Have Commas in a Uitextfield Entry
Detect Moment When Newline Starts in Uitextview
Autolayout Aspect Ratio for Uiimageview/Uiview
How to Migrate an Old Xcode Project to Use Swiftui
Autorelease Pools and When Release Is Called Under iOS