Determining your platform

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

  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

  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值