2015-01-10 157 views
6

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

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

回答

11

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

首先,我们需要知道我们正在处理的确切设备类型:

#import <sys/utsname.h> 

NSString* deviceName() 
{ 
    struct utsname systemInfo; 
    uname(&systemInfo); 

    return [NSString stringWithCString:systemInfo.machine 
           encoding:NSUTF8StringEncoding]; 
} 

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

因此,让我们将设备模型分为两组:那些图形质量较差(因此不支持模糊)的设备模型以及具有出色图形质量的设备模型。根据我的调查,这些是苹果公司认为的“差”图形设备(这些可能会在未来发生变化):

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 
} 

要小心,因为即使该设备可能支持模糊,用户可能已禁用了设置,辅助功能中的高级视觉效果。

替代方法

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

+0

使用模拟器时,'systemInfo.machine'属性是x86_64(在我的Mac上); – Tjalsma

-2

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

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 
} 

这事实上是在这里重复的问题,虽然标题可能不使它容易找到:How do I do weak linking in Swift?

- 编辑 -

误解了这个问题。那里看来是没办法短检查设备名称的发现了这一点:

Detect if device properly displays UIVisualEffectView?

我将依靠任何苹果的后备实现。看起来他们仍然应用色调,以便在大多数情况下可能很好。如果在不应用模糊时确实必须更改外观,我认为设备名称并不可怕,因为它们特别列出了哪些设备不支持模糊。

+0

这是行不通的,因为由于设备运行iOS 8,类确实存在,但上了年纪的装置,模糊视图将不会被渲染相同就像新设备一样。 – Guilherme

+0

对不起,没有注意到最后一个问题。我相信没有检查设备的名称是无法检查这一点的。 – bjtitus

2

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

  1. 的iOS版本

  2. 设备类型(感谢丹尼尔·马丁)

  3. 私人的UIDevice方法_graphicsQuality当为模拟器建造时。 (因为设备类型检查在模拟器不工作这样做 - 将建设ARM体系结构时,可以编出)

  4. “透明度降低的辅助功能设置

我做了一个含有类要点: https://gist.github.com/mortenbekditlevsen/5a0ee16b73a084ba404d

而关于这个问题作为一个整体小新手必看:

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

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

 UIAccessibilityReduceTransparencyStatusDidChangeNotification
通知,以便在辅助功能设置更改时刷新UI。

我希望有人认为这有用。 :-)

5

这是丹尼尔马丁的答案的Swift版本。一些代码改编自here

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 
} 
相关问题