6

Since I don't have iPad 2, I need to know what it returns when calling [[UIDevice currentDevice] model]. I thought it returns just "iPad" but it seems I'm wrong.

Can somebody let me know?

Thanks

woz
  • 10,888
  • 3
  • 34
  • 64
Martin
  • 69
  • 1
  • 1
  • 2
  • As noted in another [question](http://stackoverflow.com/questions/1403854/programmatically-identifing-the-iphone-device/1404080#1404080), this is usually the wrong question. – ohmantics Dec 16 '11 at 23:45

11 Answers11

27

Check for an iPad with a camera.

BOOL isIPad2 = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad &&
                [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]);

Note that it is generally better to detect specific features rather than make blanket assumptions based on model/version detection. For instance, if you need a camera, then test for the camera explicitly; if you need to tweak the UI quality based on the amount of RAM available, test for physical RAM; etc. Also note a comment I wrote that highlights the dangers of using model detection.

Community
  • 1
  • 1
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Thank you. I didn't think this far myself. :-D – Sandro Meier May 17 '11 at 19:22
  • It's a good hack ! But basing detection on the presence of a camera may cause problem with further models of ipad. I'll still be using this hack for now though. thanks ;) – oberthelot Nov 02 '11 at 14:59
  • 3
    @oberthelot: That's conceivable, but it seems unlikely that any future versions of the iPad will exclude the camera. So, unless the OP wants to exclude later models, this will work just fine. I guess the variable could be renamed `isIPad2OrLater` to make it clearer – and more likely to remain true in future. – Marcelo Cantos Nov 02 '11 at 21:07
  • Good! There is no camera on iPad1 :-D – Joey Jul 06 '12 at 06:34
  • There is a camera on "new iPad" and on "iPad mini", so this answer is plain wrong now. – hariseldon78 Jul 02 '13 at 13:41
  • @hariseldon78: I could be wrong, but my guess (then and now) is that the OP was trying to detect anything newer than the original iPad. I can't imagine any software written back then explicitly wanting to detect the iPad 2 but at the same time avoid detect anything newer. In fact, I address this very point in my comment three slots up from this one. – Marcelo Cantos Jul 02 '13 at 22:42
  • @Marcelo Cantos: ok, that's understandable, but it could very well happen that a new type of camera get mounted in future iPads, which cannot anymore use the UIImagePickerController class but needs a new one.. in that case this test will fail.. I think the better approach to this is to just check for specific features in the device, and not check for a sequential "version" – hariseldon78 Jul 03 '13 at 17:03
  • @hariseldon78: I would fall off my chair if a future iPad reported no `…SourceTypeCamera`. Nonetheless, I agree with you that feature detection is generally preferred, and I've amended my answer accordingly. – Marcelo Cantos Jul 03 '13 at 23:56
  • @Marcelo Cantos: thank you for editing, i removed the downvote.. until the day we both fall of the chair and the "noCam iPad" gets presented :) (just joking) – hariseldon78 Jul 06 '13 at 15:49
  • @user1111: That has already been discussed in earlier comments. – Marcelo Cantos Nov 29 '13 at 06:05
13

Never use the model property for anything else than displaying it for informational purposes or diagnostics output. It is not guaranteed to be preserved and if you rely on it, you unnecessarily cut off new devices as they come.

Lots of iPhone apps could not be used in the compatibility mode of iPad just because they checked the model property and if it wasn't iPhone / iPod they didn't do anything.

Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
  • 2
    Don't detect iPad 2, detect features of iPad 2. It's very likely that iPad 3 and maybe other devices will have them as well. – Tomas Vana Mar 17 '11 at 12:02
  • Getting a model is still useful to avoid checking for AGPS which takes a few seconds. For instance, if there is a iPhone in the model string, you know the device has AGPS. If you get an unknown string you can still use a fallback. – Jano May 04 '11 at 11:41
  • 1
    Some of the features of iPad n+1 (e.g. more memory, better performance) are best summed up in the words "iPad which isn't iPad n or below". I think device detection is sometimes ok as long as you future proof it by detecting from the worst device up, not from the (current) best device down, when you know there are memory or framerate issues on old devices. Capability detection is more preferable in web development or possibly in developing for other OSes (e.g. Android) where the range of environments is much wider. – Danyal Aytekin Dec 04 '11 at 11:10
9

To get the precise model string, e.g. "iPad2,2", you could use something like this.

#import "YourDeviceDetectionClass.h"
#include <sys/utsname.h>

@implementation YourDeviceDetectionClass

+(NSString*)modelAsString
{
    struct utsname platform;
    int rc = uname(&platform);
    if(rc == -1)
    {
        // Error...
        return nil;
    }
    else
    {
        // Convert C-string to NSString
        return [NSString stringWithCString:platform.machine encoding:NSUTF8StringEncoding];
    }
}

@end
Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44
7

UIScreen+Retina.h #import

@interface UIScreen(Retina)

// Returns YES if this is a Retina display.
- (BOOL)isRetina;


@end

UIScreen+Retina.m

#import "UIScreen+Retina.h"

@implementation UIScreen(Retina)

- (BOOL)isRetina {
    return [self respondsToSelector:@selector(displayLinkWithTarget:selector:)] && (self.scale == 2.0);
}

@end

USAGE

#import "UIScreen+Retina.h"

//http://stackoverflow.com/questions/3294100/how-to-differentiate-between-iphone4-and-iphone-3

if ([[UIScreen mainScreen] isRetina]) {
    // Retina display
}

IPAD/IPHONE HIGH/LOW RES

#import "UIScreen+Retina.h"
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
    //IPAD        
    if ([[UIScreen mainScreen] isRetina]) {
        // IPAD 3 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_HIGHRES;            
    }else{
        //iPAD 1/2
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_LOWRES;        }
}else{
    //IPHONE
    if ([[UIScreen mainScreen] isRetina]) {
        // IPHONE 4/4s/5 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_HIGHRES;

    }else{
        //IPHONE (3.x)
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_LOWRES;

    }
}
brian.clear
  • 5,277
  • 2
  • 41
  • 62
4

I think you can test it scale

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[[UIScreen mainScreen] scale] == 2.0) {
        // retina display
    } 
}
Hanon
  • 3,917
  • 2
  • 25
  • 29
  • The iPad 2 doesn't have a retina display. Additionally, I think display resolution isn't the right way to detect a device. – Koraktor May 22 '12 at 05:17
2

If you're using OpenGL you can look at the GPU model. iPad 1 has a PowerVR SGX 535 and iPad 2 has a PowerVR SGX 543.

const char * deviceStr = (const char *)glGetString(GL_RENDERER);
if (!strcmp(deviceStr, "PowerVR SGX 535")) {
    // iPad 1
}
else {
    // iPad 2 or later
}
blackpawn
  • 21
  • 4
1

This lines will print the device version:

Ipad 1: Platform: iPad1,1

Ipad 2: Platform: iPad2,1

Ipad 3: Platform: iPad3,3

Iphone 4S: Platform: iPhone4,1

Simulator: Platform: x86_64

size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *answer = (char*)malloc(size);
sysctlbyname("hw.machine", answer, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
free(answer);
NSLog(@"Platform: %@", platform);
Kokoima
  • 21
  • 2
1

UIDevice Class Reference should help. For more specific solutions, try this SO question.

As for your second question, the best way to test the resolution would be to acquire a display similar to the number of PPI of the iPad3,1. Unfortunately, you probably won't be able to. The best way to test any app is on the actual device.

Community
  • 1
  • 1
esqew
  • 42,425
  • 27
  • 92
  • 132
0

You can get all IPad2 return model or the model of the iOS device that you want from the following website: IOS Devices Models and Platforms

Also you can use the following code to retrieve the model direct:

- (NSString *)deviceModel
{
    struct utsname systemInfo;
    uname(&systemInfo);
    return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}

- (NSString *) platformString
{
    NSString *platform = [self deviceModel];
    if ([platform isEqualToString:@"iPhone1,1"])    return @"iPhone_2G";
    else if ([platform isEqualToString:@"iPhone1,2"])    return @"iPhone_3G";
    else if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone_3GS";
    else if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone_4";
    else if ([platform isEqualToString:@"iPhone3,3"])    return @"Verizon_iPhone_4";
    else if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone_4S";
    else if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone_5";
    else if ([platform isEqualToString:@"iPod1,1"])      return @"iPod_Touch 1G";
    else if ([platform isEqualToString:@"iPod2,1"])      return @"iPod_Touch 2G";
    else if ([platform isEqualToString:@"iPod3,1"])      return @"iPod_Touch 3G";
    else if ([platform isEqualToString:@"iPod4,1"])      return @"iPod_Touch 4G";
    else if ([platform isEqualToString:@"iPad1,1"])           return @"iPad_1G";
    else if ([platform isEqualToString:@"iPad2,1"])      return @"iPad_2(WiFi)";
    else if ([platform isEqualToString:@"iPad2,2"])      return @"iPad_2(GSM)";
    else if ([platform isEqualToString:@"iPad2,3"])      return @"iPad_2(CDMA)";
    else if ([platform isEqualToString:@"iPad3,1"])      return @"iPad_3";
    else if ([platform isEqualToString:@"iPad3,2"])      return @"iPad_3(GSM/CDMA)";
    else if ([platform isEqualToString:@"iPad3,3"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad3,4"])      return @"iPad_3(GSM)";
    else if ([platform isEqualToString:@"iPad2,5"])      return @"iPad_mini_1G";
    else if ([platform isEqualToString:@"i386"])         return @"Simulator";
    else if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    return platform;
}
Ahmed Hammad
  • 435
  • 4
  • 8
0

You are getting a lot of answers on here that state opinions on why you shouldn't be doing this and are giving you alternative code, but you aren't getting any actual answers to your question. If you want to determine exactly which device you are running on (for whatever purpose you want... don't let other developers pretend to know what you are trying to accomplish), you can using the UIDeviceHardware third-party class. You can find it here:

https://github.com/fahrulazmi/UIDeviceHardware

You'll simply call:

NSString *platformString = [UIDeviceHardware platformString];

And it will return the device. In your case, you'd be looking to match the platformString to any of these:

@"iPad 2 (WiFi)"
@"iPad 2 (GSM)"
@"iPad 2 (CDMA)"
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
0

First off I should mention that it took me a great deal of time to figure out why the ipad simulator was "saying" it was iphone. For me, it turned out I just had to switch it over to universal:

enter image description here

Here's the code which I think is fairly typical detection code for this. There are others that probably work too but...

// lifted this from the ios 4 cookbook:
- (BOOL) isiPad{

    BOOL result = NO;

    NSString *classAsString = 
    NSStringFromClass([UISplitViewController class]);

    if (classAsString == nil ||
        [classAsString length] == 0){
        return(NO);
    }

    UIDevice *device = [UIDevice currentDevice];

    if ([device respondsToSelector:
         @selector(userInterfaceIdiom)] == NO){
        return(NO);
    }

    NSLog(@"Device: %d", [[UIDevice currentDevice] userInterfaceIdiom]);
    NSLog(@"Device: %@", [[UIDevice currentDevice] model]);

    if ([device userInterfaceIdiom] != UIUserInterfaceIdiomPad){
        return(NO);
    }

    // you can put some screen size tests here too if you'd like
    result = YES;

    return(result);
}
Rob
  • 4,093
  • 5
  • 44
  • 54