我正在寻找一种方法来检测运行我的应用程序的设备。我对软件版本不感兴趣。我搜索了很多问题,但没有一个(令人惊讶地)满足我的需求,原因如下:
解决方案一:
NSString *deviceType = [UIDevice currentDevice].model;
这不起作用,因为它只给我“iPad”。我想知道是iPad、iPad 2、new iPad、iPhone 3GS、iPhone4等
解决方案 2:不测试设备类型,而是检查各个功能
这不适用,因为我希望此数据用于收集用户统计信息,而不是执行任何设备特定操作。
解决方案 3: 使用 UIDeviceHardware 找到 here
这段代码看起来很过时,而且似乎访问了设备上的私有(private)数据。甚至还有争论使用它的应用程序是否会获得批准。更重要的是,我不知道它是如何工作的 :) 还找到了一个替代方案 here
我提到的最后两个是否可以安全使用?他们 future 是否合规?它们是否符合 Apple 批准规则?
或者还有其他方法可以解决这个问题吗?
提前致谢。
最佳答案
Apple 在其示例代码中使用了此类 API。引用 LargeImageDownsizing
示例项目,在文件 LargeImageDownsizingViewController.m
从第 83 行开始:
Choosing appropriate resulting image size and tile size can be done, but is left as an exercise to the developer. Note that the device type/version string (e.g. "iPhone2,1" can be determined at runtime through use of the sysctlbyname function:
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:NSASCIIStringEncoding];
free(machine);
在我的例子中,_platform 将是 iPod4,1
for iPod touch 4th gen。
此代码是您发布的两个 Github 示例的根目录。
关于iphone - 如何检测我的用户使用的是哪个 iOS 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12224508/