返回的所有可能值是[[UIDevice currentDevice] model];
多少?它没有 记录在案。
问问题
31528 次
4 回答
41
可能的值是iPod touch
, iPhone
, iPhone Simulator
, iPad
,iPad Simulator
如果您想知道哪些硬件iOS
正在损坏,例如,iPhone3
下面是该代码iPhone4
iPhone5
注意:下面的代码可能不包含所有设备的字符串,我和其他人在GitHub 上维护相同的代码,所以请从那里获取最新的代码
目标-C:GitHub/DeviceUtil
斯威夫特:GitHub/DeviceGuru
#include <sys/types.h>
#include <sys/sysctl.h>
- (NSString*)hardwareDescription {
NSString *hardware = [self hardwareString];
if ([hardware isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
if ([hardware isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([hardware isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([hardware isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([hardware isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
if ([hardware isEqualToString:@"iPod1,1"]) return @"iPodTouch 1G";
if ([hardware isEqualToString:@"iPod2,1"]) return @"iPodTouch 2G";
if ([hardware isEqualToString:@"iPad1,1"]) return @"iPad";
if ([hardware isEqualToString:@"iPad2,6"]) return @"iPad Mini";
if ([hardware isEqualToString:@"iPad4,1"]) return @"iPad Air WIFI";
//there are lots of other strings too, checkout the github repo
//link is given at the top of this answer
if ([hardware isEqualToString:@"i386"]) return @"Simulator";
if ([hardware isEqualToString:@"x86_64"]) return @"Simulator";
return nil;
}
- (NSString*)hardwareString {
size_t size = 100;
char *hw_machine = malloc(size);
int name[] = {CTL_HW,HW_MACHINE};
sysctl(name, 2, hw_machine, &size, NULL, 0);
NSString *hardware = [NSString stringWithUTF8String:hw_machine];
free(hw_machine);
return hardware;
}
于 2013-01-19T04:33:28.500 回答
23
我刚刚在 iPod Touch、iPhone、Phone Retina、iPhone 5、iPad、iPad Retina 和 iPad Mini 上进行了测试。所以这是我的结论:
iPod touch
iPhone
iPad
在模拟器上 - 如果您是一名开发人员,开发有时在模拟器上根本不起作用的功能,这可能很有用 - 您将获得以下值:
iPhone Simulator
iPad Simulator
于 2013-05-15T14:36:54.773 回答
0
我相信解释的最佳答案(这里没有写的东西)是说值本身是一个字符串值。可能的答案是字符串,例如:“iPhone”、“iPad”等。
于 2016-09-14T19:45:25.693 回答
0
这些答案都不适用于新型号。这是一个枚举:
public enum DeviceType {
case iPad(String?)
case iPhone(String?)
case simulator(String?)
case appleTV(String?)
case unknown
}
我写的扩展我认为在新型号出现时更简洁和更可扩展。
extension UIDevice {
public static func getDevice() -> DeviceType {
var info = utsname()
uname(&info)
let machineMirror = Mirror(reflecting: info.machine)
let code = machineMirror.children.reduce("") { identifier, element in
guard let value = element.value as? Int8, value != 0 else {
return identifier
}
return identifier + String(UnicodeScalar(UInt8(value)))
}
if code.lowercased().range(of: "ipad") != nil {
if let range = code.lowercased().range(of: "ipad") {
var mutate = code
mutate.removeSubrange(range)
return .iPad(mutate)
}else{
return .iPad(nil)
}
}else if code.lowercased().range(of: "iphone") != nil {
if let range = code.lowercased().range(of: "iphone") {
var mutate = code
mutate.removeSubrange(range)
return .iPhone(mutate)
}else{
return .iPhone(nil)
}
}else if code.lowercased().range(of: "i386") != nil || code.lowercased().range(of: "x86_64") != nil{
return .simulator(code)
}else if code.lowercased().range(of: "appletv") != nil {
if let range = code.lowercased().range(of: "appletv") {
var mutate = code
mutate.removeSubrange(range)
return .appleTV(mutate)
}else{
return .appleTV(nil)
}
}else{
return .unknown
}
}
}
于 2017-10-03T01:01:56.110 回答