public
Last active

UIDeviceHardware - Determine iOS device being used

  • Download Gist
UIDeviceHardware.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13
//
// 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
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
//
// 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 stringWithUTF8String: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:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPhone3,3"]) return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
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:@"iPad2,1"]) return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([platform isEqualToString:@"i386"]) return @"Simulator";
if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
return platform;
}
 
@end
usage.txt
1 2 3
UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
[self setDeviceModel:[h platformString]];
[h release];

Thanks for the code.
I think class methods are better, to avoid class instantiation:

  • (NSString *) platform;
  • (NSString *) platformString;

And change:
NSString *platform = [self platform];
by
NSString *platform = [UIDeviceHardware platform];

Now usage is easier and more elegant

You may be right. I haven't touched xCode in since 3.2. Have at it and
branch the changes.

On 6/22/2012 10:56 AM, Ricardo1980 wrote:

Thanks for the code.
I think class methods are better, to avoid class instantiation:

  • (NSString *) platform;
  • (NSString *) platformString;

And change:
NSString *platform = [self platform];
by
NSString *platform = [UIDeviceHardware platform];

Now usage is easier and more elegant


Reply to this email directly or view it on GitHub:
https://gist.github.com/1323251

Jason Goldberg
E: jason11goldberg@gmail.com
P: 516-607-2367

Done!

El 22/06/2012, a las 18:12, Jay reply@reply.github.com escribió:

You may be right. I haven't touched xCode in since 3.2. Have at it and
branch the changes.

On 6/22/2012 10:56 AM, Ricardo1980 wrote:

Thanks for the code.
I think class methods are better, to avoid class instantiation:

  • (NSString *) platform;
  • (NSString *) platformString;

And change:
NSString *platform = [self platform];
by
NSString *platform = [UIDeviceHardware platform];

Now usage is easier and more elegant


Reply to this email directly or view it on GitHub:
https://gist.github.com/1323251

Jason Goldberg
E: jason11goldberg@gmail.com
P: 516-607-2367


Reply to this email directly or view it on GitHub:
https://gist.github.com/1323251

It seems to me this may be accessing private data of the phone - meaning non public framework apis. Has anyone got this into the app store ?

I also would like to know if anyone has gotten their app approved using this code?

I've had several apps accepted to the app store using this (or similar) code.

Have anyone plattformStrings of iPhone 5?

Aso, if anyone still wonders, all of this code uses public API. Personal data != private API.

hi is there any way to find 5.1 from the platfrom string....

Can you add iPhone 5 and the new iPod touch?

iPhone 5 is

if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5";

The new iPad:

if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM)";
if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (CDMA)";

iPad Mini is:

if ([platform isEqualToString:@"iPad2,5"]) return @"iPad mini";

iPod 5th gen
if ([platform isEqualToString:@"iPod5,1"]) return @"iPod Touch 5G";

if ([platform isEqualToString:@"iPad3,4"]) return @"iPad 4 (WiFi)";
I don't have a cell-enabled one to tell you if those return anything different. Perhaps ,5 and ,6?

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:@"iPhone3,3"])    return @"Verizon iPhone 4";
if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
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:@"iPod5,1"])      return @"iPod Touch 5G";
if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
if ([platform isEqualToString:@"i386"])         return @"Simulator";
if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

Using: http://theiphonewiki.com/wiki/Models as a guide.

hi
Can someone clarify how i can use this above code.
I am still learning xcode quickly but this confuses me as i dont know where to put it.
I dont get any errors i have just tried using NSLOG to tell me what platform is connected but it doesnt work as i cannot call the NSString platform.
I basicallt want to set variables so my program knows what device is connected.
Any help would be great
thanks
clive

Two enhancements.

  1. According to the Mac OS X Manual Page For sysctl(3), "the sysctl() function runs in about a third the time as the same request made via the sysctlbyname() function." So, the following code is probably a bit faster because it uses sysctl() instead of sysctlbyname(), but I didn't test it.

    #include <sys/sysctl.h>
    
    NSString *AACPlatform()
    {
        int mib[] = {CTL_HW, HW_MACHINE};
        size_t len = 0;
        sysctl(mib, 2, NULL, &len, NULL, 0);
        char *machine = malloc(len);
        sysctl(mib, 2, machine, &len, NULL, 0);
        NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
        free(machine);
        return platform;
    }
    
  2. You can make both Objective-C methods C functions since they don't access any instance variables.

I also see that the freebsd implementation of sysctlbyname uses syctl and sysctltomib.
(http://fxr.watson.org/fxr/source/gen/sysctlbyname.c?v=FREEBSD-LIBC).
Is Apple's implementation any different?

Any updates for iPhone 5S & 5C?

iPhone 5c : @"iPhone5,3" @"iPhone5,4"
iPhone 5s : @"iPhone6,1" @"iPhone6,2"

Reference is http://theiphonewiki.com/wiki/Models

Addition for iPad Air and iPad Mini Retina

if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (CDMA)";
if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini Retina (WiFi)";
if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini Retina (CDMA)";

froztbytes, did you mean iPad4,5 for "iPad Mini Retina (CDMA)" ?

Or maybe iPad4,3 for "iPad Mini Retina (WiFi)"

Updated list with iPhone 5c, 5s, iPad Air and Mini Retina

    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:@"iPhone3,3"])    return @"Verizon iPhone 4";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])    return @"iPhone 5 (GSM)";
    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([platform isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (Global)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (Global)";
    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:@"iPod5,1"])      return @"iPod Touch 5G";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad Mini (GSM)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad 3 (GSM)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad 4 (GSM)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (GSM)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad Mini Retina (WiFi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad Mini Retina (GSM)";
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";

I get a warning: "Implicit declaration of function 'sysctlbyname' is invalid in C99" on the line

sysctlbyname("hw.machine", NULL, &size, NULL, 0);

Hi i got an error message for this line

sysctlbyname("hw.machine", NULL, &size, NULL, 0);

Implicit declaration of function 'sysctlbyname' is invalid in C99

I am currently building for iOS7 devices only and using the latest ver of xcode 5 that is on the app store

Make sure you import the libraries:

#include <sys/types.h>
#include <sys/sysctl.h>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.