Search code examples
iosswiftios10haptic-feedbackuifeedbackgenerator

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!


Solution

  • 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.