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
- #import <UIKit/UIKit.h>
- #define IPHONE_1G_NAMESTRING @"iPhone 1G"
- #define IPHONE_3G_NAMESTRING @"iPhone 3G"
- #define IPOD_1G_NAMESTRING @"iPod touch 1G"
- #define IPOD_2G_NAMESTRING @"iPod touch 2G"
- @interface UIDevice (Hardware)
- - (NSString *) platform;
- - (NSString *) platformString;
- @end
UIDevice-hardware.m
- #import "UIDevice-hardware.h"
- #include <sys/types.h>
- #include <sys/sysctl.h>
- @implementation UIDevice (Hardware)
- /*
- Platforms
- iPhone1,1 -> iPhone 1G
- iPhone1,2 -> iPhone 3G
- iPod1,1 -> iPod touch 1G
- iPod2,1 -> iPod touch 2G
- */
- - (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];
- free(machine);
- return platform;
- }
- - (NSString *) platformString
- {
- NSString *platform = [self platform];
- if ([platform isEqualToString:@"iPhone1,1"]) return IPHONE_1G_NAMESTRING;
- if ([platform isEqualToString:@"iPhone1,2"]) return IPHONE_3G_NAMESTRING;
- if ([platform isEqualToString:@"iPod1,1"]) return IPOD_1G_NAMESTRING;
- if ([platform isEqualToString:@"iPod2,1"]) return IPOD_2G_NAMESTRING;
- return NULL;
- }
- @end
Thanks to Emanuele Vulcano
- Determining your platform
- CloudFoundry + OpenStack: Your Own Platform
- 安装SVN提示please wait while the installer finishes determining your disk space requirements
- SVN安装Please wait while the installer finishes determining your disk space requirements
- Determining your DBID--确定你的DBID(在多数据库同一个公共备份目的地的情况)
- debug your program using gdbserver in arm platform
- A guide to securing your multi-platform network.
- From Zero to Boot: Porting Android to your ARM platform
- From Zero to Boot: Porting Android to your ARM platform
- Unable to load native-hadoop library for your platform解决
- NativeCodeLoader: Unable to load native-hadoop library for your platform
- Unable to load native-hadoop library for your platform
- Unable to load native-hadoop library for your platform 问题解决
- NativeCodeLoader: Unable to load native-hadoop library for your platform
- 错误Unable to load native-hadoop library for your platform...
- 解决Unable to load native-hadoop library for your platform
- Unable to load native-hadoop library for your platform
- NativeCodeLoader: Unable to load native-hadoop library for your platform
- 揭秘DNS后台文件:DNS系列之五
- Stopwatch
- 又一次讲课
- 在Flex 3内创建一个定制化窗口环境
- Stopwatch 测速(转)
- Determining your platform
- SQLServer中的char、varchar、nchar、nvarchar四种类型
- 在spring配置文件里输入< 没有提示怎么办?
- 对员工谈绩效
- hibernate.cfg.xml配置详解
- SNMP 解析器 mibble
- Terrain Scene Manager
- 各种符号的英文
- C#精髓-- GridView 72般绝技