views:

1667

answers:

4

Hi everyone

In my application I am downloading some images from the web (from my server to be precise), in order to save some bandwith and especially memory on the phone, I provide them in two resolutions: 480x320 for the "old" iPhone series and in 960x640 for the iPhone 4 with the retina display. Now I need to be able to detect from within the app when it is running on a device that supports the retina screen. How could I do that?

I have been thinking about using the code snippet below which would return me a specific device identifier such as eg. "iPhone3", yet then I would be limiting the detection to the iPhone4 and would need to update that code for any subsequent device with a retina display.

size_t size;

// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0); 

// Allocate the space to store name
char *name = malloc(size);

// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);

// Place name into a string
NSString *machine = [NSString stringWithCString:name];

Is there any better soution (maybe it is very obvious but I missed it)?

+11  A: 

Just did some reading on the official Apple Developers Forums and the issues has been discussed in length there. The best way to me seems to be the use of the scale property of UIScreen. Although it is only available in iOS 4 and later, it will tell you everything you need to know and will most likely play an even more important role in the future (already noticed that the iPad's screen resolution of 1024x768 is exactly 32/15 * 480x320?).

If you have yet another idea feel free to post it :)

Robin
This is the correct way of doing it.
Elfred
Yes, use this, don't ever try to query which device you're running on, it will end in nothing but pain.
Joshua Weinberg
That's the correct approach, as you don't want to write conditional code for the iPhone 4 but for a display with a different scale / resolution. Who knows when the iPod will get the retina treatment.
Felix
@Felix: hopefully in the next update, expected in January. And wouldn't it be just agonizingly nifty to have our code magically support it without any tweakage? Hence the [UIScreen scale] as the best approach.
Dan Ray
+1  A: 

Go with Robin's answer. One other note: if you do need to check the device name, just use the methods on UIDevice.

[[UIDevice currentDevice] model];
[[UIDevice currentDevice] systemName];
[[UIDevice currentDevice] systemVersion];
Matt Rix
+5  A: 

Here is some code to do it the right way for both iOS 3.x and 4.x:

BOOL hasHighResScreen = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
    CGFloat scale = [[UIScreen mainScreen] scale];
    if (scale > 1.0) {
        hasHighResScreen = YES;
    }
}
Scott Gustafson
Just beware that this will also return true for the iPad's "iphone simulator" mode if it starts at 2x, which may or may not be what you want.
Matt Rix