在API10中,有一个新的iOS,它允许开发人员使用taptic引擎UIFeedbackGenerator。
虽然此应用程序接口在iOS 10中可用,但它仅适用于新设备iPhone 7和7 plus。它不适用于包括6S或6S Plus在内的老款设备,即使是那些有taptic引擎的设备也不能使用。我猜7和7 plus上的taptic引擎是一个不同的更强大的引擎。
我似乎找不到一种方法来查看设备是否支持使用新的api。我想用taptic代码替换一些振动代码,这是有意义的。
编辑:
添加3个具体的子类用于搜索: UIImpactFeedbackGenerator UINotificationFeedbackGenerator UISelectionFeedbackGenerator
编辑2:
我有一个理论,但没有iPhone 7设备来测试它,所以如果你有一个,试一试。UIFeedbackGenerator有一个名为prepare()的方法。当打印出UIImpactFeedbackGenerator的一个实例时,我注意到它打印了一个名为"prepared“的属性,该属性将显示0。在模拟器或iPhone 6S上调用prepare(),然后打印出实例仍然显示为0。是否有人可以从iPhone7调用UIImpactFeedbackGenerator实例上的prepare(),然后将该实例打印到控制台,以查看prepared是否设置为1?此值未公开,但可能有一种方法可以在没有使用私有apis的情况下获取此信息。
发布于 2016-09-20 18:58:58
显然,这可以通过一个私有API调用来完成。
Objective-C:
[[UIDevice currentDevice] valueForKey:@"_feedbackSupportLevel"];
Swift:
UIDevice.currentDevice().valueForKey("_feedbackSupportLevel");
...这些方法似乎返回了:
0 =磁带而不是available1 =第一代(在iPhone 6s上测试) ...它不支持UINotificationFeedbackGenerator,etc.2 =第二代(在iPhone 7上测试) ...支持哪个。不幸的是,这里有两个警告:
使用这些的
特别感谢Tim Oliver和Steve T-S在不同设备上帮助测试。https://twitter.com/TimOliverAU/status/778105029643436033
发布于 2016-12-23 17:04:44
目前,最好的方法是使用以下命令检查设备的型号:
public extension UIDevice
public func platform() -> String {
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
}iPhone 7和7 plus的平台名称为:"iPhone9,1", "iPhone9,3", "iPhone9,2", "iPhone9,4"
来源:iOS: How to determine the current iPhone/device model in Swift?
您可以创建一个函数:
public extension UIDevice {
public var hasHapticFeedback: Bool {
return ["iPhone9,1", "iPhone9,3", "iPhone9,2", "iPhone9,4"].contains(platform())
}
}发布于 2018-12-29 19:11:57
我已经扩展了chrisamanse's answer。它从型号标识符中提取代号,并检查它是否等于或大于9。除非苹果决定引入新的内部命名方案,否则它应该适用于未来的iPhone型号。
public extension UIDevice {
var modelIdentifier: String {
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
var hasHapticFeedback: Bool {
// assuming that iPads and iPods don't have a Taptic Engine
if !modelIdentifier.contains("iPhone") {
return false
}
// e.g. will equal to "9,5" for "iPhone9,5"
let subString = String(modelIdentifier[modelIdentifier.index(modelIdentifier.startIndex, offsetBy: 6)..<modelIdentifier.endIndex])
// will return true if the generationNumber is equal to or greater than 9
if let generationNumberString = subString.components(separatedBy: ",").first,
let generationNumber = Int(generationNumberString),
generationNumber >= 9 {
return true
}
return false
}
}像这样使用它:
if UIDevice.current.hasHapticFeedback {
// work with taptic engine
} else {
// fallback for older devices
}https://stackoverflow.com/questions/39564510
复制相似问题