如何检查是否支持触觉引擎 (UIFeedbackGenerator)

How to check if Haptic Engine (UIFeedbackGenerator) is supported

我想知道如何检查新的 iOS 10 API UIFeebackGenerator 在当前设备上是否可用。还有一些我们需要检查的东西:

  1. 设备需要 运行 iOS 10.0 或更高版本
  2. 设备需要是 iPhone 7 或更高版本
  3. 需要在设置中打开触觉引擎

前两项检查可以使用 #available(iOS 10, *) 语句和(hacky)设备检测来实现,但后一项似乎不可检查。

有人知道这个的解决方案吗?或者我们可能需要为此提交一个 Apple Radar。谢谢!

基于 Apple's UIFeedbackGenerator documentation,听起来 iOS 可以为您做到这一点。

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.

即使您不需要担心检查设备是否可以进行触觉反馈,您仍然需要确保它仅在 iOS 10 或更大时被调用,因此您可以通过以下方式实现:

    if #available(iOS 10,*) {
        // your haptic feedback method
    }

iOS 10.

有一些未记录的 "private thing":

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

returns 2 对于具有触觉反馈的设备 - iPhone 7/7+ 因此您可以轻松地使用它来生成触觉反馈:

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

returns 1 for iPhone 6S,这里是生成触觉的回退:

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)

returns 0 对于 iPhone 6 或更旧的设备。由于它是一种未记录的东西,它可能会在审查阶段阻止你,尽管我能够通过审查并提交带有此类检查的应用程序。

更多详情: http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/

您知道您的设备是否支持触觉振动效果,请使用以下代码,

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

这些方法似乎return:

  • 0 = 触觉不可用

  • 1 = 第一代(在 iPhone 6s 上测试)...... 支持UINotificationFeedbackGenerator等

  • 2= 第二代(在 iPhone 7 上测试)...确实支持 它.

它 returns 2 对于具有触觉反馈的设备 - iPhone 7/7+ 或更高,因此,您可以轻松地使用它来生成触觉反馈

这适用于 iPhone 7 及以上。

 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
    }
}

我对 UIDevice 进行了扩展,但没有使用私有 API

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

你可以这样使用它:

UIDevice.isHapticsSupported 

returns truefalse

iOS 13以来,您可以通过非常直接的方式查看它。根据文档页面,你所要做的就是:

import CoreHaptics

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

文档:Preparing Your App to Play Haptics