Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

ios - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...