2

I POSTED MY ANSWER FOR THIS.

Please check.

I am able to check programatically if I am running my app on Simulator or not. But I want to know what simulator I am running like... 1. iPhone Retina (3.5 inch) OR 2. iPhone Retina (4 inch 64 bit) OR ... x. iPad Retina etc.

Please help.

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";
Chandu
  • 630
  • 2
  • 8
  • 18

3 Answers3

1

Maul and Divya... This is how I check...

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;

Now, you can include the checks if height (variable h) is 960 or 1136 => iPhone Or, if height is 2048 => iPad

In my code... just to mimic that I am running the app on the physical iPad/iPhone... I return the string...

iPad 4
iPhone 5

etc.

LET ME KNOW WHAT YOU THINK, tell me if I can improve the code.

Chandu
  • 630
  • 2
  • 8
  • 18
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)
    }
}

}

This will be easy to test the device types both on simulator and device. Unless you need which type of phone it is (GSM/CDMA) For checking device type we does not need to check which type of carrier the device have.

Charan Giri
  • 1,097
  • 1
  • 9
  • 15
-2

Use following code like below, and its working for me .

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

Here is reference : How can I programmatically determine if my app is running in the iphone simulator?

Community
  • 1
  • 1
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45