iOS:如何检查我的应用程序运行的模拟器?

时间:2013-12-19 12:42:42

标签: ios iphone ios-simulator

我为此发布了我的答案。

请检查。

如果我在模拟器上运行我的应用程序,我可以以编程方式检查。 但我想知道我正在运行的模拟器... 1. iPhone Retina(3.5英寸)或 2. iPhone Retina(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";

3 个答案:

答案 0 :(得分:1)

Maul和Divya ......这就是我检查......

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 =>,您可以包括支票。苹果手机 或者,如果高度为2048 => ipad公司

在我的代码中...只是为了模仿我在物理iPad / iPhone上运行应用程序......我返回字符串......

iPad 4
iPhone 5

让我知道你在想什么,告诉我是否可以改进代码。

答案 1 :(得分: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)为了检查设备类型,我们不需要检查设备有哪种类型的手机。

答案 2 :(得分:-2)

使用下面的代码,它适用于我。

#if TARGET_IPHONE_SIMULATOR
NSString *deviceType  = @"iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *deviceType = @" Device";
#else
NSString *deviceType = @"Other";
#endif

以下是参考:How can I programmatically determine if my app is running in the iphone simulator?

相关问题