2014-04-08 28 views
-2

我是iOS開發新手。我如何知道我的應用程序目前安裝的設備。我想要在iOS模擬器上檢查當前設備是iPad還是iPhone的代碼。決定設備類型的應用程序正在運行

+0

可能重複[如何檢測iPhone 5(寬屏設備)?] (http://stackoverflow.com/questions/12446990/how-to-detect-iphone-5-widescreen-devices) –

回答

0

的最好的方式找到該設備是iPad或iPhone是

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{ 
    // The device is an iPad running iPhone 3.2 or later. 
} 
else 
{ 
    // The device is an iPhone or iPod touch. 
} 

如果你想在什麼樣的設備的更多詳細信息,請參閱https://gist.github.com/Jaybles/1323251

+0

不工作我想知道,該設備是iPhone4 ipnoe5或Ipad .. – user3418619

+0

@ user3418619然後清楚你說的朋友,看看我的編輯。 – satheeshwaran

0
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{ 
    // The device is an iPad 
} 
else 
{ 
    // The device is an iPhone 

    if(([[UIScreen mainScreen] bounds].size.height>520)) 
    { 
     // The device is an iPhone5 
    } 
    else 
    { 
     // The device is an iPhone4 
    } 
} 
+0

除了檢測您是否在iPad或iPhone/iPod Touch上運行,我不會將iPhone 5視爲特例,而是編寫代碼以使用任何屏幕尺寸運行。 – gnasher729

+0

user3418619評論satheeshwaran答案,「我想知道該設備是iphone4 ipnoe5或ipad ..」 –

0

添加該代碼,它可以幫助:

#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE 
#define isiPhone (UI_USER_INTERFACE_IDIOM() == 0)?TRUE:FALSE 

而不是用作

if(isiPhone) 
      {  

       if (isiPhone5) 
       { 

        //Code for iphone 5 

       } 
       else 
       { 
        //iphone 3.5 inch screen 

       }  
      } 
      else 
      { 
       //[ipad] 

      } 

快樂編碼

0

使用此

+ (NSString *)yesButWhichDeviceIsIt 
{ 
BOOL hasRetina = NO; 
if ([UIScreen instancesRespondToSelector:@selector(scale)]) { 
    CGFloat scale = [[UIScreen mainScreen] scale]; 
    if (scale > 1.0) { 
     hasRetina = YES; 
    } 
} 
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
    if (hasRetina) { 
     return @"iPad retina"; 
    } else { 
     return @"iPad"; 
    } 
} 
else { 
    if (hasRetina) { 
     if ([[UIScreen mainScreen] bounds].size.height == 568){ 
      return @"iPhone5"; 
     } 
     else 
     { 
      return @"iPhone4s"; 
     } 
    } else { 
     return @"iPhone"; 
    } 
} 
} 

這裏返回類型爲String,這將給你的設備類型的

+0

你忘了在你的代碼中處理iPhone 4。 iPhone 5和4S是不夠的。 – klcjr89

相關問題