12

How do I detect if a user is running the application on an iPhone 4 or 3G/3GS?

I need to detect the hardware, not the iOS version.

thanks for any help.

Duck
  • 34,902
  • 47
  • 248
  • 470
  • You can call [currentDevice](http://developer.apple.com/iphone/library/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/uid/TP40006902-CH3-SW10) on UIDevice and look at the model property. **Edit:** Although... the docs suggest this doesn't include the exact model number. – Joost Schuur Jul 05 '10 at 07:09

3 Answers3

11

feel free to use this class - I found it here

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
Community
  • 1
  • 1
SeniorShizzle
  • 984
  • 1
  • 11
  • 27
  • 1
    Much better than my answer, but someone needs to run this on an iPhone 4 and see what the machine string is for that model. I think it might be "iPhone3,1". – Joost Schuur Jul 05 '10 at 07:28
  • +1 Good point. I'm too tired now, but I'll try tomorrow and post the results-- unless anybody beats me to it – SeniorShizzle Jul 05 '10 at 07:38
  • I'd just use hw.machine (and hw.model). iPhone1,1 is also known as "Original iPhone" or "iPhone 2G" (unofficialy) and Apple lists "iPod Touch 2nd generation" and "iPod Touch 3rd generation", one of which is missing here (presumably iPod2,2) – tc. Jul 05 '10 at 12:01
  • and what about the iPad? and this method fails to detect the correct version of the simulator. Any clues? – Duck Jul 05 '10 at 14:56
  • It's a bit laborious, but has the platform strings listed for each iOS device. For example, for iPod touch 4th gen. I have not been able to find a list indexed on platform string. – westsider Oct 07 '10 at 17:38
6
#import <sys/utsname.h>

+ (NSString*)deviceModelName {

        struct utsname systemInfo;

        uname(&systemInfo);

        NSString *modelName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

        if([modelName isEqualToString:@"i386"]) {
            modelName = @"iPhone Simulator";
        }
        else if([modelName isEqualToString:@"iPhone1,1"]) {
            modelName = @"iPhone";
        }
        else if([modelName isEqualToString:@"iPhone1,2"]) {
            modelName = @"iPhone 3G";
        }
        else if([modelName isEqualToString:@"iPhone2,1"]) {
            modelName = @"iPhone 3GS";
        }
        else if([modelName isEqualToString:@"iPhone3,1"]) {
            modelName = @"iPhone 4";
        }
        else if([modelName isEqualToString:@"iPhone4,1"]) {
            modelName = @"iPhone 4S";
        }
        else if([modelName isEqualToString:@"iPod1,1"]) {
            modelName = @"iPod 1st Gen";
        }
        else if([modelName isEqualToString:@"iPod2,1"]) {
            modelName = @"iPod 2nd Gen";
        }
        else if([modelName isEqualToString:@"iPod3,1"]) {
            modelName = @"iPod 3rd Gen";
        }
        else if([modelName isEqualToString:@"iPad1,1"]) {
            modelName = @"iPad";
        }
        else if([modelName isEqualToString:@"iPad2,1"]) {
            modelName = @"iPad 2(WiFi)";
        }
        else if([modelName isEqualToString:@"iPad2,2"]) {
            modelName = @"iPad 2(GSM)";
        }
        else if([modelName isEqualToString:@"iPad2,3"]) {
            modelName = @"iPad 2(CDMA)";
        }

        return modelName;
    }
Vishnu Kumar. S
  • 1,797
  • 1
  • 15
  • 35
  • Thats simple ... after the last 'if else' you can add else { modelName=@"iPhone 5";} – Vishnu Kumar. S Sep 21 '12 at 05:38
  • 1
    Don't use an `else` without an `if` for the iPhone 5. Any unknown devices will incorrectly fallback to being called an iPhone 5. – kirb Dec 10 '12 at 14:53
1

You can use this code for detecting iPhone OS version. float version = [[[UIDevice currentDevice] systemVersion] floatValue];

if (version >= 3.0) {
    // Only executes on version 3 or above.
} 
IOS Rocks
  • 2,127
  • 2
  • 21
  • 24