views:

143

answers:

2

My iPhone app (http://hexalex.com) is not universal but it has a feature that I'd like to enable for people playing on iPads. Is there some way to detect that you're running on an iPad in compatibility mode? The UIDevice methods for detecting machine specs all return the values you would get on an iPhone (on the simulator at least). The only thing I can think of is detecting OS 3.2, but that technique won't work for long.

+4  A: 

1) Use UIDevice-Extension written by Erica Sadun. A very comprehensive class: http://github.com/erica/uidevice-extension/blob/master/UIDevice-Hardware.m

2) Or you could also use the UIDevice class method:

[[UIDevice currentDevice] name]              // eg. "Brock's iPhone"
[[UIDevice currentDevice] model]             // eg. @"iPhone", @"iPod Touch"
[[UIDevice currentDevice] localizedModel]    // localized version of model
[[UIDevice currentDevice] systemName]        // eg. @"iPhone OS"
[[UIDevice currentDevice] systemVersion]     // eg. @"3.2"
[[UIDevice currentDevice] uniqueIdentifier]  // UDID, a unique string to identify the device

Each of the above lines will return an NSString. To which you can do a string comparison like so:

NSString *model = [[UIDevice currentDevice] model];
NSLog(@"Current device model: \"%@\"", model);

3) Another way:

http://www.drobnik.com/touch/2009/07/determining-the-hardware-model/ You will need to modify this to use the right hardware number for the iPad. Taken from the link above:

UIDevice-hardware.h

#import 

#define IPHONE_1G_NAMESTRING @"iPhone 1G"
#define IPHONE_3G_NAMESTRING @"iPhone 3G"
#define IPHONE_3GS_NAMESTRING @"iPhone 3GS"
#define IPOD_1G_NAMESTRING @"iPod touch 1G"
#define IPOD_2G_NAMESTRING @"iPod touch 2G"

@interface UIDevice (Hardware)
- (NSString *) platform;
- (NSString *) platformString;
@end

UIDevice-hardware.m

#import "UIDevice-hardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>

@implementation UIDevice (Hardware)

/*
 Platforms
 iPhone1,1 = iPhone 1G
 iPhone1,2 = iPhone 3G
 iPhone2,1 = iPhone 3GS
 iPod1,1   = iPod touch 1G
 iPod2,1   = iPod touch 2G
 */

- (NSString *) platform
{
 size_t size;
 sysctlbyname("hw.machine", NULL, &amp;size, NULL, 0);
 char *machine = malloc(size);
 sysctlbyname("hw.machine", machine, &amp;size, NULL, 0);
 NSString *platform = [NSString stringWithCString:machine];
 free(machine);
 return platform;
}

- (NSString *) platformString
{
 NSString *platform = [self platform];
 if ([platform isEqualToString:@"iPhone1,1"]) return IPHONE_1G_NAMESTRING;
 if ([platform isEqualToString:@"iPhone1,2"]) return IPHONE_3G_NAMESTRING;
 if ([platform isEqualToString:@"iPhone2,1"]) return IPHONE_3GS_NAMESTRING;
 if ([platform isEqualToString:@"iPod1,1"])   return IPOD_1G_NAMESTRING;
 if ([platform isEqualToString:@"iPod2,1"])   return IPOD_2G_NAMESTRING;
 return NULL;
}
@end
Brock Woolf
I don't have an iPad to test on, but in the iPad simulator `[[UIDevice currentDevice] model]` returns `iPhone Simulator`, not `iPad Simulator`. I assume it will return `iPhone` instead of `iPad` when running on the device. It's an emulated iPhone environment, so this is no surprise.The `hw.machine` sysctl is an interesting suggestion, but on the iPad simulator it just returns `i386`. Do you know what it returns on the iPad when running a non-universal iPhone app?
n8gray
You shouldn't need to test this on the simulator. You need a real device, otherwise how are you to know wthat happens in the field environment?
Brock Woolf
A: 

Did you check the "UIDevice.h" ? It has model property which u can find-out the iPhone,iPod,iPad devices

NSString *name; // e.g. "My iPhone"

NSString *model; // e.g. @"iPhone", @"iPod Touch"

NSString *localizedModel; // localized version of model

NSString *systemName; // e.g. @"iPhone OS"

NSString *systemVersion; // e.g. @"2.0"

NSString *uniqueIdentifier; // a string unique to each device based on various hardware info.

Adeem Maqsood Basraa
I don't have an iPad to test on, but in the iPad simulator `[[UIDevice currentDevice] model]` returns `iPhone Simulator`, not `iPad Simulator`.
n8gray
I print [[UIDevice currentDevice] model] and it returned me "iPad":)
Adeem Maqsood Basraa
@n8gray: yes simulator is limited. only works on iOS devices.
Brock Woolf
Accepted for telling me what it does on the actual device.
n8gray
@n8gray: You didn't accept my answer?
Brock Woolf