iPad 2 detection
Asked Answered
I

11

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

Immunotherapy answered 17/3, 2011 at 10:41 Comment(1)
As noted in another question, this is usually the wrong question.Simferopol
H
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.

Halfbreed answered 26/4, 2011 at 1:25 Comment(9)
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 ;)Parik
@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.Halfbreed
Good! There is no camera on iPad1 :-DExuberate
There is a camera on "new iPad" and on "iPad mini", so this answer is plain wrong now.Diller
@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.Halfbreed
@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"Diller
@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.Halfbreed
@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)Diller
@user1111: That has already been discussed in earlier comments.Halfbreed
E
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.

Evonneevonymus answered 17/3, 2011 at 10:50 Comment(3)
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.Evonneevonymus
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.Spectrograph
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.Formulaic
F
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
Formulaic answered 4/12, 2011 at 11:6 Comment(1)
Works great! Better than any other solution out there. Thanks pal!Perfumery
W
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"

//https://mcmap.net/q/120142/-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;

    }
}
Worcestershire answered 8/3, 2012 at 11:4 Comment(0)
S
4

I think you can test it scale

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[[UIScreen mainScreen] scale] == 2.0) {
        // retina display
    } 
}
Strenta answered 8/3, 2012 at 5:23 Comment(1)
The iPad 2 doesn't have a retina display. Additionally, I think display resolution isn't the right way to detect a device.Outpost
N
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
}
Nitride answered 16/12, 2011 at 23:58 Comment(0)
Y
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.

Yours answered 8/3, 2012 at 5:22 Comment(0)
F
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);
Foresight answered 9/4, 2012 at 8:39 Comment(0)
G
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);
}
Gestate answered 25/8, 2011 at 0:52 Comment(0)
A
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;
}
Anatomy answered 9/11, 2012 at 13:13 Comment(0)
D
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)"
Diva answered 24/11, 2016 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.