簡體   English   中英

如何在iOS 8.3中檢測設備是否為iPad?

[英]How can I detect if device is an iPad in iOS 8.3?

我們將SDK更新到iOS 8.3,突然之間,我們的iPad檢測方法無法正常工作:

+ (BOOL) isiPad
{
#ifdef UI_USER_INTERFACE_IDIOM
    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
#endif
    return NO;
}

永遠不會輸入ifdef塊,所以return NO; 總是運行。 如何在不使用UI_USER_INTERFACE_IDIOM()情況下檢測設備是否為iPad?


我正在使用:

  • Xcode 6.3(6D570)
  • iOS 8.2(12D508) - 使用iOS 8.3編譯器進行編譯
  • 部署:目標設備系列:iPhone / iPad
  • Mac OS X:約塞米蒂(10.10.3)
  • Mac:MacBook Pro(MacBookPro11,3)

8.2 UserInterfaceIdiom()

#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

8.3 UserInterfaceIdiom()

static inline UIUserInterfaceIdiom UI_USER_INTERFACE_IDIOM() {
    return ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ?
            [[UIDevice currentDevice] userInterfaceIdiom] :
            UIUserInterfaceIdiomPhone);
}

所以#ifdef UI_USER_INTERFACE_IDIOM8.3始終為false

請注意標題說

提供了UI_USER_INTERFACE_IDIOM()函數,以便在部署到小於3.2的iOS版本時使用。 如果您要部署的最早版本的iPhone / iOS是3.2或更高版本,您可以直接使用 - [UIDevice userInterfaceIdiom]。

所以建議你重構一下

+ (BOOL) isiPad
{
    static BOOL isIPad = NO;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        isIPad = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
    });
    return isIPad;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM