0

When I want an iPhone to vibrate, how can I determine whether to use UIFeedbackGenerator or AudioServicesPlaySystemSound?

The new haptic engine is only available on iOS10 and on the iPhone 7 models, so how would we write code that uses this new engine when supported, but falls back to using the not as cool AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) when not?

Apple says to not worry about it, and that UIFeedbackGenerator simply won't do anything if not supported, but I don't see how this help me if I still want it to vibrate since its methods do not have any kind of success callback.

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.

As a general rule, trust the system to determine whether it should play feedback. Don't check the device type or app state to conditionally trigger feedback. After you’ve decided how you want to use feedback, always trigger it when the appropriate events occur. The system ignores any requests that it cannot fulfill.

Community
  • 1
  • 1
Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
  • `init` the `UIFeedbackGenerator` object, if the object is `nil` then you are not on the preferred engine, fallback to `AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)`. – Sachin Vas Jan 31 '17 at 08:25
  • I thought UIFeedbackGenerator is a part of iOS10, and can, therefore, be initiated no matter what device you are on? – Daniel Larsson Jan 31 '17 at 08:39
  • In your question it reads `only available on iOS10` **and** `on the iPhone 7 models,` – Sachin Vas Jan 31 '17 at 08:41
  • The haptic engine, yes. The UIFeedbackGenerator is a part of UIKit on iOS10, so from what I understand, you can still try to use it. I just won't have any effect unless coupled with a newer device. – Daniel Larsson Jan 31 '17 at 08:43
  • Yeah, but the hardware has to support those API's right. https://www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator – Sachin Vas Jan 31 '17 at 08:44
  • In the link you posted: "At this time, only the new Taptic Engine found in the iPhone 7 and iPhone 7 Plus support these APIs. Other devices silently ignore the haptic requests." The fact that other devices silently ignores the requests makes me believe that initiating an object would still work – Daniel Larsson Jan 31 '17 at 08:46
  • Sorry I lost track what was the question, use https://gist.github.com/imkevinxu/2bb1197552b095ab25c8 to check if the device is `iPhone 7 and iPhone 7 Plus` along with `available API for iOS 10`... If not use the `AudioServices API` – Sachin Vas Jan 31 '17 at 08:51

0 Answers0