Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Haptic Engine (UIFeedbackGenerator) is supported

I am wondering how we could check if the new iOS 10 API UIFeebackGenerator is available on the current device. There are some more things we would need to check:

  1. The device needs to run iOS 10.0 or later
  2. The device needs to be an iPhone 7 or later
  3. The Haptic Engine needs to be turned on in the Settings

The first two checks can be achieved using #available(iOS 10, *) statement and a (hacky) device-detection, but the latter one doesn't seem to be checkable.

Does someone know a solution for this? Or maybe we need to file an Apple Radar for this one. Thanks!

like image 397
Hans Knöchel Avatar asked Jan 03 '17 13:01

Hans Knöchel


People also ask

Which devices support haptic feedback?

Simple haptic devices are common in the form of game controllers, joysticks, and steering wheels.

Does the haptic engine wear out?

It's like magic. Nope, the button does not move but the haptic motor kind of gives the sensation of it being pressed. I love it over the old home button. Nothing to wear out and it works so well.

How do you turn on a haptic engine?

How to turn off or on haptic feedback. Please also note that you can change your iPhone's 3D or Haptic Touch sensitivity by going to Settings > Accessibility > Touch, then tap 3D & Haptic Touch (or 3D Touch or Haptic Touch only).


1 Answers

There's some undocumented "private thing":

UIDevice.currentDevice().valueForKey("_feedbackSupportLevel"); 

it returns 2 for devices with haptic feedback - iPhone 7/7+ so you can easily use this to generate Haptic feedback:

let generator = UIImpactFeedbackGenerator(style: .heavy) generator.prepare() generator.impactOccurred() 

returns 1 for iPhone 6S, here's a fallback to generate taptic:

import AudioToolbox  AudioServicesPlaySystemSound(1519) // Actuate `Peek` feedback (weak boom) AudioServicesPlaySystemSound(1520) // Actuate `Pop` feedback (strong boom) AudioServicesPlaySystemSound(1521) // Actuate `Nope` feedback (series of three weak booms) 

and returns 0 for iPhone 6 or older devices. Since it's kind of undocumented thing it might block you during the review stage, although I was able to pass review and submit the app with such check.

More details: http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/

like image 89
Mikita Manko Avatar answered Sep 25 '22 03:09

Mikita Manko