我想知道如何检查新的iOS 10 API UIFeebackGenerator
在当前设备上是否可用。还有一些我们需要检查的事情:
前两项检查可以使用#available(iOS 10, *)
语句和(漏洞)设备检测来实现,但后一项似乎不可检查。
有人知道解决方案吗?或者也许我们需要为此提交Apple Radar。谢谢!
基于Apple's UIFeedbackGenerator
documentation,听起来像iOS为您做到了。
请注意,调用这些方法不会直接播放触觉。而是将事件通知系统。 然后确定是否基于设备播放触觉,应用程序的状态,剩余电量以及其他因素。
例如,当前仅播放触觉反馈:
[在具有受支持的Taptic Engine的设备上(iPhone 7和iPhone 7加)。
[当应用程序在前台运行时。
当启用系统触觉设置时。
即使您不必担心检查设备是否可以进行触觉反馈,您仍然需要确保仅在iOS 10或更高版本上才能调用该设备,因此您可以通过以下方式完成此操作:
UIFeedbackGenerator
这里是iOS 10中可用的 if #available(iOS 10,*) {
// your haptic feedback method
}
。
有一些未记录的“私人物品”:
a quick summary of the various haptic feedback options
it returns 2用于带触觉反馈的设备-iPhone 7/7 +,因此您可以轻松地使用它生成触觉反馈:
UIDevice.currentDevice().valueForKey("_feedbackSupportLevel");
returns 1对于iPhone 6S,这是生成Taptic的后备选项:
let generator = UIImpactFeedbackGenerator(style: .heavy)
generator.prepare()
generator.impactOccurred()
对于iPhone 6或更低版本的设备,和返回0。由于它是一种未记录的事物,因此它可能会在审阅阶段阻止您,尽管我能够通过审阅并提交带有此支票的应用程序。
更多详细信息: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)
我在不使用私有API的情况下扩展了http://www.mikitamanko.com/blog/2017/01/29/haptic-feedback-with-uifeedbackgenerator/的信息>
UIDevice
}你这样使用它:
extension UIDevice { static var isHapticsSupported : Bool { let feedback = UIImpactFeedbackGenerator(style: .heavy) feedback.prepare() return feedback.description.hasSuffix("Heavy>") }
返回
UIDevice.isHapticsSupported
或true
使用以下代码,您知道设备是否支持触觉振动效果,
这将适用于iPhone 7及更高版本。