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
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
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.
isIPad2OrLater
to make it clearer – and more likely to remain true in future. –
Halfbreed …SourceTypeCamera
. Nonetheless, I agree with you that feature detection is generally preferred, and I've amended my answer accordingly. –
Halfbreed 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.
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
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;
}
}
I think you can test it scale
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
if ([[[UIScreen mainScreen] scale] == 2.0) {
// retina display
}
}
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
}
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.
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);
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:
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);
}
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;
}
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)"
© 2022 - 2024 — McMap. All rights reserved.