2014-09-13 53 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

相關問題