2013-12-19 212 views
2

我在這裏發表了我的回答。iOS:如何檢查我的App正在運行的模擬器?

請檢查。

我能夠以編程方式檢查,如果我在模擬器上運行我的應用程序或不。 但我想知道我正在運行什麼模擬器... 1. iPhone視網膜(3.5英寸)或 2. iPhone視網膜(4英寸64位)或 ... x。 iPad Retina 等

請幫忙。

size_t size; 
sysctlbyname("hw.machine", NULL, &size, NULL, 0); 
char *machine = malloc(size); 
sysctlbyname("hw.machine", machine, &size, NULL, 0); 
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; 
free(machine); 

if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5"; 
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (GSM+CDMA)"; 
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5S"; 
... 
... 
if ([platform isEqualToString:@"i386"])   return @"Simulator"; 
if ([platform isEqualToString:@"x86_64"])  return @"Simulator"; 
+0

嗨,大家好,這已經解決了。其實我是通過設備類找到它的。無論如何感謝您的回覆。多謝你們! – Chandu

+0

如何將此問題移至解決狀態? – Chandu

+0

在此發佈您的答案並將其核對爲已接受,以便其他人可以在將來使用它。謝謝 – Maul

回答

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"; // simulator 4 inch 
     } 
     else 
     { 
      return @"iPhone4s"; simulator 3.5 inch 
     } 
    } else { 
     return @"iPhone"; simulator(normal) 
    } 
} 

}

這將是容易測試的設備類型上都模擬器和設備。除非您需要使用哪種類型的手機(GSM/CDMA)。對於檢查設備類型,我們不需要檢查設備具有哪種類型的運營商。

+0

。不要誤導訪問者。 – Mani

+0

@Mani其實這個工作完美...我用這個開發40個應用程序的。我編輯我的答案檢查它 –

+0

@mani這個作品 –

1

毆打和迪夫亞...這是我如何檢查...

CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

//Check the scale (I use it to see the pixel density 
CGFloat screenScale = [[UIScreen mainScreen] scale]; 

NSInteger w=(unsigned)screenBounds.size.width * screenScale; 
NSInteger h=(unsigned)screenBounds.size.height * screenScale; 

// I don't want to check the device orientation. 
//So, make always height greater than width. 
(w>h)?(w=(w+h)-(h=w)):1; 

現在,你可以包括檢查高度(變量h)是960或1136 => iPhone 或者,如果身高是2048 => iPad

在我的代碼中...只是爲了模仿我在物理iPad/iPhone上運行應用程序......我退回n個串...

iPad 4 
iPhone 5 

讓我知道你在想什麼,告訴我,如果我可以提高代碼。