2014-09-13 55 views
2

是否有方法可以确定运行iOS TodayExtension的设备的设备类型。我无法在Swift中做到这一点。如何在Swift中获取设备类型TodayExtension

我需要有这样的事情:

-(BOOL)isiPad { 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     return YES; 
    } else { 
     return NO; 
    } 
} 

但是在斯威夫特和一个TodayExtension。

编辑:

let IS_IPAD = (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad); 

未在iOS中TodayExtensions工作。

enter image description here

+0

比较http://stackoverflow.com/questions/24831850/how-should-i-replace-these-screen-size-and-device-type-macros-in-swift。 – 2014-09-13 18:14:36

+0

@MartinR UIUserInterfaceIdomPad在TodayExtensions上不工作 – 2014-09-13 20:03:09

回答

-4

我只是理解了它自己。这里是代码...

let iPad: [CGFloat]! = [2048, 1024] 
let iPhone: [CGFloat]! = [1920, 1334, 1136] 

func isIpad() -> Bool { 
    if (find(iPad, UIScreen.mainScreen().bounds.height) != nil) { 
     return true 
    } else { 
     return false 
    } 
} 
+0

由于新iOS模型被添加/更改,这可能很容易返回不正确的结果。 – 2015-07-16 21:39:25

7

在Swift中,您可以使用此代码。

let isiPad: Bool = (UIDevice.currentDevice().userInterfaceIdiom == .Pad); 

在我看来,用这个作为常量。

if (isiPad) { 
    println(isiPad) 
} 
else { 
    println(isiPad) 
} 
+0

In swift 3 and above 'UIDevice.currentDevice()。userInterfaceIdiom == .Pad' become 'UIDevice.current.userInterfaceIdiom == .pad' – 2018-02-20 03:25:33

相关问题