tags:

views:

9484

answers:

9

Is there a way to determine the device running an application. I want to distinguish between iPhone and iPod Touch if possible.

+1  A: 

Check for GPS or the camera.

eduffy
That's probably the least reliable way of doing this, if you want to even consider this a way.
Dutchie432
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
Both comments are valid. It depends what you are looking for. A camera or an /iPod touch.
Moshe
I'd just like to point out that Apple has now released an iPod Touch with a camera.
Josh Hinman
+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
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
@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
@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
@jeeva, i dont think that is supported in 3.0 OS
Anil Sivadas
+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
thanks for the code ...
Biranchi
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
I honestly couldn't tell you. I have not done any iPad development.
Dutchie432
@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
A: 

Is the UIDeviceHardware class using undocumented and so forbidden libraries?

Zdrien
Nope, they're documented. http://developer.apple.com/iphone/library/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysctlbyname.3.html
lawrence
+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
Another minor update adding the 4th gen iPod and iPad:
Brian Robbins
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
A: 

this line of code crashes my app

[self setDeviceModel:[h platformString]];

the only warning i get in the .h file is that

NSString *platform = [NSString stringWithCString:machine];

//StingWithCString is deprecated

+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