2011年11月2日 星期三

[iPhone] Check iOS device type/ device family

Reference from : http://www.cocos2d-iphone.org/forum/topic/8233

To get the device family for iPhone or iPad or iPod touch, use UIDevice is not enough, it cannot get the really device type. Need to use sysctlbyname() for more information.

/////////////////////////////////////////////////////////////////////////////////////

In DeviceHardware.h file

// Predefine for different devices
#define IPHONE_1G @"iPhone1,1"
#define IPHONE_3G @"iPhone1,2"
#define IPHONE_3GS @"iPhone2,1"
#define IPHONE_4 @"iPhone3,1"
#define IPAD @"iPad1,1"
#define IPOD_TOUCH_1G @"iPod1,1"
#define IPOD_TOUCH_2G @"iPod2,1"
#define IPOD_TOUCH_3G @"iPod3,1"
#define I386 @"Simulator"


@interface DeviceHardware : NSObject


+ (NSString *) platform;

/////////////////////////////////////////////////////////////////////////////////////

In DeviceHardware.m

#import "DeviceHardware.h"
#include
#include


@implementation DeviceHardware

+ (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 encoding:NSUTF8StringEncoding];
free(machine);

return platform;
}


/////////////////////////////////////////////////////////////////////////////////////

Make a little change from the original one. With predefinition, it might be easier to use. If anyone has better solution, please feel free to let me know!

沒有留言:

張貼留言