Manbolo Blog

Manbolo Team Blog, creators of MeonArchives

UIDevice Hardware Category

If you need a human readable description of an iOS device, check out UIDevice+Hardware category on GitHub. Previously, I was using Erica Sadun’s various categories on UIDevice but the last commit happened a year ago. UIDevice+Hardware seems simpler and more up to date.

The core of the code relies on Apple’s internal naming:

- (NSString*)hardwareString
    size_t size = 100;
    char *hw_machine = malloc(size);
    int name[] = {CTL_HW,HW_MACHINE};
    sysctl(name, 2, hw_machine, &size, NULL, 0);
    NSString *hardware = [NSString stringWithUTF8String:hw_machine];
    return hardware;

The code is straightforward but could be a lot simpler by using a lookup table. Instead of this:

- (NSString*)hardwareSimpleDescription
    NSString *hardware = [self hardwareString];
    if ([hardware isEqualToString:@"iPhone1,1"])    return @"iPhone 2G";
    if ([hardware isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([hardware isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([hardware isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([hardware isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([hardware isEqualToString:@"iPhone3,3"])    return @"iPhone 4";
    if ([hardware isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([hardware isEqualToString:@"iPhone5,1"])    return @"iPhone 5";

    return nil;

Why not simply use dictionary literals?

- (NSString*)hardwareSimpleDescription
    NSString *hardware = [self hardwareString];
    if (!hardware) return nil;

    NSDictionary *hardwareToSimple = @{
                                        @"iPhone1,1": @"iPhone 2G",
                                        @"iPhone1,2": @"iPhone 3G", 
                                        @"iPhone2,1": @"iPhone 3GS",
                                        @"iPhone3,1": @"iPhone 4", 
    return hardwareToSimple[hardware];

For the record, the current list of iOS devices is:

iPhone1,1iPhone 2G
iPhone1,2iPhone 3G
iPhone2,1iPhone 3GS
iPhone3,1iPhone 4
iPhone3,2iPhone 4
iPhone3,3iPhone 4
iPhone4,1iPhone 4S
iPhone5,1iPhone 5
iPhone5,2iPhone 5
iPhone5,3iPhone 5c
iPhone5,4iPhone 5c
iPhone6,1iPhone 5s
iPhone6,2iPhone 5s
iPod1,1iPod Touch (1 Gen)
iPod2,1iPod Touch (2 Gen)
iPod3,1iPod Touch (3 Gen)
iPod4,1iPod Touch (4 Gen)
iPod5,1iPod Touch (5 Gen)
iPad2,1iPad 2
iPad2,2iPad 2
iPad2,3iPad 2
iPad2,4iPad 2
iPad2,5iPad mini
iPad2,6iPad mini
iPad2,7iPad mini
iPad3,1iPad 3
iPad3,2iPad 3
iPad3,3iPad 3
iPad3,4iPad 4
iPad3,5iPad 4
iPad3,6iPad 4
iPad4,1iPad Air
iPad4,2iPad Air
iPad4,3iPad Air
iPad4,4iPad mini Retina
iPad4,5iPad mini Retina

From jc.

All Posts