检查设备是否支持模糊

我的应用程序使用UIBlurEffect ,但是较旧的设备(特别是支持iOS 8的iPad 2和3)不支持模糊。

我想检查用户的设备是否有模糊支持。 我该怎么做?

UIDevice有一个看起来很有前途的内部方法[UIDevice _graphicsQuality] ,但是当然你的应用程序将被苹果拒绝。 我们来创build我们自己的方法:

首先,我们需要知道我们正在使用的确切设备types:

 #import <sys/utsname.h> NSString* deviceName() { struct utsname systemInfo; uname(&systemInfo); return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; } 

例如,这应该返回iPad2,1 iPad 2。 以下是iDevice模型的更新列表: https : //theiphonewiki.com/wiki/Models

因此,我们将我们的设备模型分为两类:graphics质量差(因此不支持模糊)的设备模型,以及具有很高graphics质量的设备模型。 根据我的调查,这些是苹果认为“差”graphics的设备(这些可能会在未来改变):

iPad iPad1,1 iPhone1,1 iPhone1,2 iPhone2,1 iPhone3,1 iPhone3,2 iPhone3,3 iPod1,1 iPod2,1 iPod2,2 iPod3,1 iPod4,1 iPad2,1 iPad2,2 iPad2,3 iPad2,4 iPad3 ,1个iPad3,2 iPad3,3

所以我们写下面的代码:

 NSSet *graphicsQuality = [NSSet setWithObjects:@"iPad", @"iPad1,1", @"iPhone1,1", @"iPhone1,2", @"iPhone2,1", @"iPhone3,1", @"iPhone3,2", @"iPhone3,3", @"iPod1,1", @"iPod2,1", @"iPod2,2", @"iPod3,1", @"iPod4,1", @"iPad2,1", @"iPad2,2", @"iPad2,3", @"iPad2,4", @"iPad3,1", @"iPad3,2", @"iPad3,3", nil]; if ([graphicsQuality containsObject:deviceName()]) { // Device with poor graphics, blur not supported } else { // Blur supported } 

请小心,因为即使设备可能支持模糊,用户也可能已禁用“设置”,“辅助function”中的高级视觉效果。

替代方法

https://gist.github.com/conradev/8655650

这是Daniel Martin答案的Swift版本。 一些代码是从这里改编的:

 func isBlurSupported() -> Bool { var supported = Set<String>() supported.insert("iPad") supported.insert("iPad1,1") supported.insert("iPhone1,1") supported.insert("iPhone1,2") supported.insert("iPhone2,1") supported.insert("iPhone3,1") supported.insert("iPhone3,2") supported.insert("iPhone3,3") supported.insert("iPod1,1") supported.insert("iPod2,1") supported.insert("iPod2,2") supported.insert("iPod3,1") supported.insert("iPod4,1") supported.insert("iPad2,1") supported.insert("iPad2,2") supported.insert("iPad2,3") supported.insert("iPad2,4") supported.insert("iPad3,1") supported.insert("iPad3,2") supported.insert("iPad3,3") return !supported.contains(hardwareString()) } func hardwareString() -> String { var name: [Int32] = [CTL_HW, HW_MACHINE] var size: Int = 2 sysctl(&name, 2, nil, &size, &name, 0) var hw_machine = [CChar](count: Int(size), repeatedValue: 0) sysctl(&name, 2, &hw_machine, &size, &name, 0) let hardware: String = String.fromCString(hw_machine)! return hardware } 

非常感谢丹尼尔·马丁的伟大答案。 我已经做了一个UIDevice类别,结合以下检查模糊效果的可用性:

  1. iOS版本

  2. 设备types(感谢Daniel Martin)

  3. 私有UIDevice方法_graphicsQuality在为模拟器构build时。 (这样做是因为设备types检查在模拟器中不起作用 – 在构buildarm架构时将被编译出来)

  4. “降低透明度”可访问性设置

我已经做了一个包含类别的要点: https : //gist.github.com/mortenbekditlevsen/5a0ee16b73a084ba404d

关于整个问题的一个小写作:

http://mojoapps.dk/?p=1

注意:使用要点时,不要忘记订阅

  UIAccessibilityReduceTransparencyStatusDidChangeNotification 

当可访问性设置更改时,通知刷新UI。

我希望有人认为这有用。 🙂

这里有几个方法来做到这一点:

 if objc_getClass("UIBlurEffect") != nil { // UIBlurEffect exists } else { // UIBlurEffect doesn't exist } 

或者访问blurEffect类

 if let blurEffect: AnyClass = NSClassFromString("UIBlurEffect") { // UIBlurEffect exists } else { // UIBlurEffect doesn't exist } 

其实这里有一个重复的问题,虽然标题可能不容易find: 我如何在Swift中做弱连接?

– 编辑 –

误解了这个问题。 似乎没有办法find这个短的检查设备名称:

检测设备是否正确显示UIVisualEffectView?

我会依靠苹果的后备实施。 看起来他们仍然应用色调,所以在大多数情况下可能是好的。 如果你真的必须改变外观没有模糊将被应用,我认为设备名称并不可怕,因为他们具体列出哪些设备不支持模糊。