Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch if possible.
That's probably the least reliable way of doing this, if you want to even consider this a way.
Dutchie432
2009-10-13 17:56:27
Actually checking for the feature you want is MORE reliable than checking the model. You write your app, you put it out in the wild, it has a hardcoded check to say "if iPod touch then no camera". Apple puts out an iPod Touch (someday!!) that has a camera, your app is broken.
jsd
2009-10-17 03:08:27
Both comments are valid. It depends what you are looking for. A camera or an /iPod touch.
Moshe
2010-04-02 03:16:46
I'd just like to point out that Apple has now released an iPod Touch with a camera.
Josh Hinman
2010-10-07 18:07:18
+20
A:
You can use the UIDevice
class as so:
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"])
// it's an iPhone
Adam Rosenfield
2009-01-15 19:54:04
Apple given UIUserInterfaceIdiomPad for ipad don't use model... check this link for nice utility method http://cocoabugs.blogspot.com/2010/09/checking-device-is-ipad-or-iphone-in.html
jeeva
2010-09-15 04:51:36
@jeeva: The code in that link is a compile-time check for Universal apps (apps which are compiled separately for iPhone and iPad). This code is a runtime check. Also keep in mind that this question and answer were written long before the iPad ever existed.
Adam Rosenfield
2010-09-15 15:17:43
@Adam i agree with you.. you have have written a answer very long back that is nice work .... i told instead of using model you can use UIUserInterfaceIdiomPad which is given for that purpose only...
jeeva
2010-09-16 04:26:38
+17
A:
feel free to use this class
Usage
UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
[self setDeviceModel:[h platformString]];
[h release];
UIDeviceHardware.h
//
// UIDeviceHardware.h
//
// Used to determine EXACT version of device software is running on.
#import <Foundation/Foundation.h>
@interface UIDeviceHardware : NSObject
- (NSString *) platform;
- (NSString *) platformString;
@end
UIDeviceHardware.m
//
// UIDeviceHardware.m
//
// Used to determine EXACT version of device software is running on.
#import "UIDeviceHardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@implementation UIDeviceHardware
- (NSString *) platform{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &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";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
return platform;
}
@end
Dutchie432
2009-10-13 18:01:40
Does this work with iPad as well? I know there would need to be another if statement, but the question is, does the iPad store it's information the same way? (IE: if ([platformisEqualToString:@"ipad1,1"]) return @"iPad 1G"; )Thanks for this!
Scott
2010-03-08 21:59:58
@Scott - try using the simulator form the beta sdk. (Or wait a week...) EDIT: The simulator may not return a correct value, wait until the iPad comes out in a few days. I would assume it does store its info that way. Apple Computers model numbers report in the system information program as "MacBook 2,1" for example. Or "iMac 10,1". So, I would assume Apple followed this model.
Moshe
2010-04-02 03:15:44
A:
Is the UIDeviceHardware class using undocumented and so forbidden libraries?
Zdrien
2009-12-02 16:22:20
Nope, they're documented. http://developer.apple.com/iphone/library/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysctlbyname.3.html
lawrence
2009-12-09 17:25:08
+1
A:
Here's a minor update with new models:
- (NSString *) platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
return platform;
}
sss
2010-07-23 18:55:53
A:
Another minor update adding the 4th Gen iPod and iPad
- (NSString *) platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G";
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad";
if ([platform isEqualToString:@"i386"]) return @"Simulator";
return platform;
}
Brian Robbins
2010-10-16 20:53:54
+1
A:
More usable
#include <sys/types.h>
#include <sys/sysctl.h>
@interface UIDevice(Hardware)
- (NSString *) platform;
- (BOOL)hasRetinaDisplay;
- (BOOL)hasMultitasking;
- (BOOL)hasCamera;
@end
@implementation UIDevice(Hardware)
- (NSString *) platform{
int mib[2];
size_t len;
char *machine;
mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
sysctl(mib, 2, NULL, &len, NULL, 0);
machine = malloc(len);
sysctl(mib, 2, machine, &len, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
free(machine);
return platform;
}
- (BOOL)hasRetinaDisplay {
NSString *platform = [self platform];
BOOL ret = YES;
if ([platform isEqualToString:@"iPhone1,1"]) {
ret = NO;
}
else
if ([platform isEqualToString:@"iPhone1,2"]) ret = NO;
else
if ([platform isEqualToString:@"iPhone2,1"]) ret = NO;
else
if ([platform isEqualToString:@"iPod1,1"]) ret = NO;
else
if ([platform isEqualToString:@"iPod2,1"]) ret = NO;
else
if ([platform isEqualToString:@"iPod3,1"]) ret = NO;
return ret;
}
- (BOOL)hasMultitasking {
if ([self respondsToSelector:@selector(isMultitaskingSupported)]) {
return [self isMultitaskingSupported];
}
return NO;
}
- (BOOL)hasCamera {
BOOL ret = NO;
// check camera availability
return ret;
}
@end
you can reading properties with
NSLog(@"platform %@, retita %@, multitasking %@", [[UIDevice currentDevice] platform], [[UIDevice currentDevice] hasRetinaDisplay] ? @"YES" : @"NO" , [[UIDevice currentDevice] hasMultitasking] ? @"YES" : @"NO");
Gaj
2010-10-18 10:39:47