Determining your platform

来源:互联网 发布:python netsnmp模块 编辑:程序博客网 时间:2024/06/03 08:38

iPhone dev: Determining your platform

There are real differences between the first and second generation iPod touch. The second generation iPod presents built-in speakers, a faster processor, and other hardware that your application may need to know about. When you want to check the capabilities for the platform you're running on, you turn to the UIDevice class, right? Not so fast.

The UIDevice class can tell you which platform you're running on, be it the iPhone or iPod touch, but it won't tell you much more than that. At this time, there's no way to check if that iPod touch or iPhone is the first generation or second.

To help you out, we've expanded the UIDevice class. The following code returns a string that tells you exactly which platform your application is running on. This works by calling sysctlbyname. Sysctlbyname retrieves information made available to the running operating system and can return those values to your program.

As far as determining which platform you're running on, you recover that information by querying the hw.machine value. The hw.machine key returns one of four strings:

  • iPhone1,1 (for the first gen iPhone)
  • iPhone1,2 (for the 3G model)
  • iPod1,1 (first gen iPod touch)
  • iPod2,1 (second gen iPod touch)

These return strings show you exactly which hardware your program is using and let you adjust your application options accordingly.

I have created a new repository at github that contains the following class extension code. You can freely download it and use it in your program. Although I've included a skeleton testbed at the repository, all you need to use in general are the two UIDevice-hardware files.

These files declare two new methods (platform and platformString) which return the raw sysctl values and more user-readable strings. These latter constants are defined in the header file and can be used in your application.

UIDevice-hardware.h

view plainprint?
  1. #import <UIKit/UIKit.h>
  2. #define IPHONE_1G_NAMESTRING @"iPhone 1G"
  3. #define IPHONE_3G_NAMESTRING @"iPhone 3G"
  4. #define IPOD_1G_NAMESTRING @"iPod touch 1G"
  5. #define IPOD_2G_NAMESTRING @"iPod touch 2G"
  6. @interface UIDevice (Hardware)
  7. - (NSString *) platform;
  8. - (NSString *) platformString;
  9. @end

UIDevice-hardware.m

view plainprint?
  1. #import "UIDevice-hardware.h"
  2. #include <sys/types.h>
  3. #include <sys/sysctl.h>
  4. @implementation UIDevice (Hardware)
  5. /*
  6. Platforms
  7. iPhone1,1 -> iPhone 1G
  8. iPhone1,2 -> iPhone 3G
  9. iPod1,1 -> iPod touch 1G
  10. iPod2,1 -> iPod touch 2G
  11. */
  12. - (NSString *) platform
  13. {
  14. size_t size;
  15. sysctlbyname("hw.machine", NULL, &size, NULL, 0);
  16. char *machine = malloc(size);
  17. sysctlbyname("hw.machine", machine, &size, NULL, 0);
  18. NSString *platform = [NSString stringWithCString:machine];
  19. free(machine);
  20. return platform;
  21. }
  22. - (NSString *) platformString
  23. {
  24. NSString *platform = [self platform];
  25. if ([platform isEqualToString:@"iPhone1,1"]) return IPHONE_1G_NAMESTRING;
  26. if ([platform isEqualToString:@"iPhone1,2"]) return IPHONE_3G_NAMESTRING;
  27. if ([platform isEqualToString:@"iPod1,1"]) return IPOD_1G_NAMESTRING;
  28. if ([platform isEqualToString:@"iPod2,1"]) return IPOD_2G_NAMESTRING;
  29. return NULL;
  30. }
  31. @end

Thanks to Emanuele Vulcano

原创粉丝点击