Posted By

espinallab on 08/07/10


Tagged

iphone SDK 4 ipod touch


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

alfredbaudisch
umang_nine
bobbym245
rvachere
cnathan


Determine device (iPhone, iPod Touch) with iPhone SDK


 / Published in: Objective C
 

URL: http://stackoverflow.com/questions/448162/determine-device-iphone-ipod-touch-with-iphone-sdk

Here is how to determine the device type.\r\n\r\nHow to use it.\r\n\r\nUIDeviceHardware *h=[[UIDeviceHardware alloc] init];\r\n[self setDeviceModel:[h platformString]]; \r\n[h release];

  1. //
  2. // UIDeviceHardware.h
  3. //
  4. // Used to determine EXACT version of device software is running on.
  5.  
  6. #import <Foundation/Foundation.h>
  7.  
  8. @interface UIDeviceHardware : NSObject
  9.  
  10. - (NSString *) platform;
  11. - (NSString *) platformString;
  12.  
  13. @end
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. //
  21. // UIDeviceHardware.m
  22. //
  23. // Used to determine EXACT version of device software is running on.
  24.  
  25. #import "UIDeviceHardware.h"
  26. #include <sys/types.h>
  27. #include <sys/sysctl.h>
  28.  
  29. @implementation UIDeviceHardware
  30.  
  31. - (NSString *) platform{
  32. size_t size;
  33. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  34. char *machine = malloc(size);
  35. sysctlbyname("hw.machine", machine, &size, NULL, 0);
  36. NSString *platform = [NSString stringWithCString:machine];
  37. free(machine);
  38. return platform;
  39. }
  40.  
  41. - (NSString *) platformString{
  42. NSString *platform = [self platform];
  43. if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
  44. if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
  45. if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
  46. if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
  47. if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
  48. if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
  49. if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
  50. if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
  51. return platform;
  52. }
  53.  
  54. @end

Report this snippet  

You need to login to post a comment.