How to test which iOS Hardware your app is running on

There are two ways to test which hardware your app is currently running on: compiler time and runtime. Both have their advantages.


Here’s how we can check both:

Compile Time Device Check

We can use the currentDevice method of our UIDevice class:

// determine current device (compile time)
NSString *currentDevice = [UIDevice currentDevice].model;

This will give you a string such as “iPhone”, “iPad” or “iPod Touch”.

Runtime Device Check

This is slightly more complex, but I’m sure you’ve heard of identifiers such as “iPhone6,1” or “iPad 3,3” – that’s Apple’s internal numbering system for iOS devices. With this check we can get very fine grained information about the hardware capabilities a device will have.

To use it, we need to first import the sysctrl framework into our app like so:

#import <sys/sysctl.h>

Then we can use some C methods that reads out this magic identifier. Here’s how:

- (NSString *) platformString {
    // Gets a string with the device model
    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 encoding:NSUTF8StringEncoding];

    if ([platform isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone4,1"])    return @"iPhone 4S (GSM)";

    if ([platform isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (CDMA)";
    if ([platform isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM+CDMA)";
    if ([platform isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (US/Japan)";
    if ([platform isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (Europe/Asia)";

    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod Touch (4 Gen)";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (WiFi)";
    return platform;

First we grab the string of this magic identifier, then we compare it to a LONG list of possible values and extract the model number. The list above is not complete and it’s just meant for demo purposes. You get the idea.

The sysctrl functions are documented here:

Demo Project

I have a working demo project on GitHub which has a (relatively complete) list of current iOS device models. Check it out here:

Kudos to the following links that made this possible:

Leave a Reply