Alex Rivera | Logout

Determine user's device using iOS SDK and full Cocoa Touch / Objective-C code

Asked 2011-11-06T02:28:20.607
8

I have written the following UIDevice category based on different sources. I've upgraded the platformCode method so it's less low-level than can be seen.

This works perfectly, but the platformCode method is low level. Do you know if this kind of call can be replaced with Cocoa Touch code? Here's the relevant code:

UIDevice_enhanced.h

@interface UIDevice (Enhanced)

typedef enum {
    kUnknownPlatform = 0,
    kiPhone1G,
    kiPhone3G,
    kiPhone3GS,
    kiPhone4,
    kiPhone4Verizon,
    kiPhone4S,
    kiPodTouch1G,
    kiPodTouch2G,
    kiPodTouch3G,
    kiPodTouch4G,
    kiPad,
    kiPad2Wifi,
    kiPad2GSM,
    kiPad2CMDA,
    kSimulator
} PlatformType;

- (NSString *) platformName;
- (PlatformType) platform;

@end

UIDevice_enhanced.m

#import "UIDevice_enhanced.h"
#include <sys/utsname.h>

@interface UIDevice (Enhanced)
- (NSString *) platformCode;
@end 


@implementation UIDevice (Enhanced)

// Utility method (private)
- (NSString*) platformCode {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString* platform =  [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

    return platform;
}

// Public method to use
- (NSString*) platformName {
    NSString* platform = [self platformCode];

    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,2"])    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 ([p
Edit
Report

1 Answer

1

You'll have to use the low level C call to get the infoString. For my purposes I've written a tiny Objective-C library that abstracts this away and presents an Objective-C interface.

NSLog(@"Big model number: %d", deviceDetails.bigModel);
//Big model number: 4

NSLog(@"Small model number: %d", deviceDetails.smallModel);
//Small model number: 1

if (deviceDetails.model == GBDeviceModeliPhone4S) {
    NSLog(@"It's a 4S");
}
//It's a 4S

if (deviceDetails.family != GBDeviceFamilyiPad) {
    NSLog(@"It's not an iPad");
}
//It's not an iPad

NSLog(@"systemInfo string: %@", [GBDeviceInfo rawSystemInfoString]);
//systemInfo string: iPhone4,1

You can get it on github if you like: GBDeviceInfo

answered 2012-09-20T09:47:04.323

Your Answer