27

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!

Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34
Hans Knöchel
  • 11,422
  • 8
  • 28
  • 49

6 Answers6

21

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/

Mikita Manko
  • 1,133
  • 9
  • 9
17

Starting from iOS 13 you can check it in a very straightforward way. According to documentation page, all you have to do is:

import CoreHaptics

var supportsHaptics: Bool = false
...
// Check if the device supports haptics.
let hapticCapability = CHHapticEngine.capabilitiesForHardware()
supportsHaptics = hapticCapability.supportsHaptics

Documentation: Preparing Your App to Play Haptics

Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34
15

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.

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 12
    What if you want to fall back to using the old AudioServicesPlaySystemSound if the taptic engine is not available? – Daniel Larsson Jan 31 '17 at 01:05
  • @DanielLarsson This might be helpful. You'd want to check for iOS before version 10. http://nshipster.com/swift-system-version-checking/ – Adrian Jan 31 '17 at 17:11
  • 1
    @Adrian just tested with iOS 9, Seems that it just don't do anything (doesn't crash the app). – Idan May 09 '17 at 10:19
5

I made an extension to UIDevice without using the private API

extension UIDevice {
    
        static var isHapticsSupported : Bool {
            let feedback = UIImpactFeedbackGenerator(style: .heavy)
            feedback.prepare()
            return feedback.description.hasSuffix("Heavy>")
        }
    }

and you use it like this :

UIDevice.isHapticsSupported 

returns true or false

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Rom4in
  • 532
  • 4
  • 13
2

You know your device support Haptic vibration effect or not with below code,

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

These methods seem to return:

  • 0 = Taptic not available

  • 1 = First generation (tested on an iPhone 6s) ... which does NOT support UINotificationFeedbackGenerator, etc.

  • 2= Second generation (tested on an iPhone 7) ... which does support it.

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

Ashish
  • 2,977
  • 1
  • 14
  • 32
  • 1
    Sorry, but that is a private API and will cause rejections unless obfuscated. But for in-house apps, it's a usable solution! – Hans Knöchel Apr 27 '18 at 14:36
  • Using these could get your app rejected by Apple during the App Store's App Review, but there doesn't seem to be any other way currently. – Ashish Feb 25 '19 at 08:48
0

This will work for iPhone 7 and Above.

 var count = 0

 override func viewDidLoad() {
    super.viewDidLoad()

    let myButton = UIButton(frame: CGRect(x: 0, y: 100, width: 100, height: 50))
    myButton.setTitleColor(UIColor.green, for: .normal)
    myButton.setTitle("Press ME", for: .normal)
    myButton.addTarget(self, action: #selector(myButtonTapped), for: .touchUpInside)
    self.view.addSubview(myButton)

}

@objc func myButtonTapped() {
    count += 1
    print("Count \(count)")

    switch count {
    case 1:
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.error)

    case 2:
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success)

    case 3:
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.warning)

    case 4:
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.impactOccurred()

    case 5:
        let generator = UIImpactFeedbackGenerator(style: .medium)
        generator.impactOccurred()

    case 6:
        let generator = UIImpactFeedbackGenerator(style: .heavy)
        generator.impactOccurred()

    default:
        let generator = UISelectionFeedbackGenerator()
        generator.selectionChanged()
        count = 0
    }
}
Yogendra Singh
  • 2,063
  • 25
  • 20