Determine device (iPhone, iPod Touch) with iPhone SDK


/ Published in: Objective C
Save to your folder(s)

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];


Copy this code and paste it in your HTML
  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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.