Possible Duplicate:
Determine device (iPhone, iPod Touch) with iOS
我正在制作一款利用 iPhone(可能还有第二代 iPod touch)的点对点蓝牙功能的游戏。但是,为了阻止用户尝试在 iPod 1st gen 和 iPhone 2G 上玩多人游戏,我需要检查特定的设备型号。
[[UIDevice currentDevice] model] 只会告诉我设备是“iPhone”还是“iPod touch”。有没有办法检查特定的设备型号,例如:“iPhone 3GS”、“iPod touch 1st generation”之类的。
编辑:
UIDevice 有一个类别(我认为它是由 Erica Sadun 创建的,我不相信它),它使用以下代码来获取特定的设备型号。你可以在这里找到整个类别以及其他有用的东西:https://github.com/erica/uidevice-extension
#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 encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}
此功能有效,使用此功能的应用最近已在 AppStore 中获得批准。
最佳答案
您可以使用 sys/utsname.h
中的 uname
获取设备型号。例如:
#import <sys/utsname.h>
NSString*
machineName()
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
}
结果应该是:
@"i386" on the simulator @"iPod1,1" on iPod Touch @"iPod2,1" on iPod Touch Second Generation @"iPod3,1" on iPod Touch Third Generation @"iPod4,1" on iPod Touch Fourth Generation @"iPhone1,1" on iPhone @"iPhone1,2" on iPhone 3G @"iPhone2,1" on iPhone 3GS @"iPad1,1" on iPad @"iPad2,1" on iPad 2 @"iPad3,1" on iPad 3 (aka new iPad) @"iPhone3,1" on iPhone 4 @"iPhone4,1" on iPhone 4S @"iPhone5,1" on iPhone 5 @"iPhone5,2" on iPhone 5
https://stackoverflow.com/questions/1108859/
相关文章:
ios - 从 iPhone 上的 NSString 中删除 HTML 标签
objective-c - Objective-C 中 `oneway void` 的用例?
ios - 链接器命令失败,退出代码 1(使用 -v 查看调用),Xcode 8,Swift 3
objective-c - 在 Objective-C 中向 nil 发送消息
objective-c - 是否可以更改 UIButtons 背景颜色?
ios - 如果没有 Table View 结果,在屏幕上显示 "No Results"
ios - 在 ios9 中替换 stringByAddingPercentEscapesUsing