//
// UIDevice+Hardware.h
// gauravds
//
#import <UIKit/UIKit.h>
@interface UIDevice (Hardware)
- (NSString *)modelIdentifier;
- (NSString *)modelName;
@end
为什么不创建一个分类呢?不要使用malloc方法安全泄露问题。使用安全的函数。
NSLog(@"%@ %@", [[UIDevice currentDevice] modelName], [[UIDevice currentDevice] modelIdentifier]);
查看实现
//
// UIDevice+Hardware.m
// gauravds
//
#import "UIDevice+Hardware.h"
#import <sys/utsname.h>
@implementation UIDevice (Hardware)
- (NSString *)modelIdentifier {
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}
- (NSString *)modelName {
return [self modelNameForModelIdentifier:[self modelIdentifier]];
}
- (NSString *)modelNameForModelIdentifier:(NSString *)modelIdentifier {
// iPhone http://theiphonewiki.com/wiki/IPhone
if ([modelIdentifier isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([modelIdentifier isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([modelIdentifier isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([modelIdentifier isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (GSM)";
if ([modelIdentifier isEqualToString:@"iPhone3,2"]) return @"iPhone 4 (GSM Rev A)";
if ([modelIdentifier isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (CDMA)";
if ([modelIdentifier isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
if ([modelIdentifier isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (GSM)";
if ([modelIdentifier isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (Global)";
if ([modelIdentifier isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (GSM)";
if ([modelIdentifier isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (Global)";
if ([modelIdentifier isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (GSM)";
if ([modelIdentifier isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (Global)";
if ([modelIdentifier isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
if ([modelIdentifier isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
if ([modelIdentifier isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
if ([modelIdentifier isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
if ([modelIdentifier isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
if ([modelIdentifier isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
if ([modelIdentifier isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
if ([modelIdentifier isEqualToString:@"iPhone9,3"]) return @"iPhone 7";
if ([modelIdentifier isEqualToString:@"iPhone9,4"]) return @"iPhone 7 Plus";
if ([modelIdentifier isEqualToString:@"iPhone10,1"]) return @"iPhone 8"; // US (Verizon), China, Japan
if ([modelIdentifier isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus"; // US (Verizon), China, Japan
if ([modelIdentifier isEqualToString:@"iPhone10,3"]) return @"iPhone X"; // US (Verizon), China, Japan
if ([modelIdentifier isEqualToString:@"iPhone10,4"]) return @"iPhone 8"; // AT&T, Global
if ([modelIdentifier isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus"; // AT&T, Global
if ([modelIdentifier isEqualToString:@"iPhone10,6"]) return @"iPhone X"; // AT&T, Global
// iPad http://theiphonewiki.com/wiki/IPad
if ([modelIdentifier isEqualToString:@"iPad1,1"]) return @"iPad 1G";
if ([modelIdentifier isEqualToString:@"iPad2,1"]) return @"iPad 2 (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)";
if ([modelIdentifier isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)";
if ([modelIdentifier isEqualToString:@"iPad2,4"]) return @"iPad 2 (Rev A)";
if ([modelIdentifier isEqualToString:@"iPad3,1"]) return @"iPad 3 (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad3,2"]) return @"iPad 3 (GSM)";
if ([modelIdentifier isEqualToString:@"iPad3,3"]) return @"iPad 3 (Global)";
if ([modelIdentifier isEqualToString:@"iPad3,4"]) return @"iPad 4 (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad3,5"]) return @"iPad 4 (GSM)";
if ([modelIdentifier isEqualToString:@"iPad3,6"]) return @"iPad 4 (Global)";
if ([modelIdentifier isEqualToString:@"iPad6,11"]) return @"iPad (5th gen) (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad6,12"]) return @"iPad (5th gen) (Cellular)";
if ([modelIdentifier isEqualToString:@"iPad4,1"]) return @"iPad Air (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad4,2"]) return @"iPad Air (Cellular)";
if ([modelIdentifier isEqualToString:@"iPad5,3"]) return @"iPad Air 2 (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad5,4"]) return @"iPad Air 2 (Cellular)";
// iPad Mini http://theiphonewiki.com/wiki/IPad_mini
if ([modelIdentifier isEqualToString:@"iPad2,5"]) return @"iPad mini 1G (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad2,6"]) return @"iPad mini 1G (GSM)";
if ([modelIdentifier isEqualToString:@"iPad2,7"]) return @"iPad mini 1G (Global)";
if ([modelIdentifier isEqualToString:@"iPad4,4"]) return @"iPad mini 2G (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad4,5"]) return @"iPad mini 2G (Cellular)";
if ([modelIdentifier isEqualToString:@"iPad4,6"]) return @"iPad mini 2G (Cellular)"; // TD-LTE model see https://support.apple.com/en-us/HT201471#iPad-mini2
if ([modelIdentifier isEqualToString:@"iPad4,7"]) return @"iPad mini 3G (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad4,8"]) return @"iPad mini 3G (Cellular)";
if ([modelIdentifier isEqualToString:@"iPad4,9"]) return @"iPad mini 3G (Cellular)";
if ([modelIdentifier isEqualToString:@"iPad5,1"]) return @"iPad mini 4G (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad5,2"]) return @"iPad mini 4G (Cellular)";
// iPad Pro https://www.theiphonewiki.com/wiki/IPad_Pro
if ([modelIdentifier isEqualToString:@"iPad6,3"]) return @"iPad Pro (9.7 inch) 1G (Wi-Fi)"; // http://pdadb.net/index.php?m=specs&id=9938&c=apple_ipad_pro_9.7-inch_a1673_wifi_32gb_apple_ipad_6,3
if ([modelIdentifier isEqualToString:@"iPad6,4"]) return @"iPad Pro (9.7 inch) 1G (Cellular)"; // http://pdadb.net/index.php?m=specs&id=9981&c=apple_ipad_pro_9.7-inch_a1675_td-lte_32gb_apple_ipad_6,4
if ([modelIdentifier isEqualToString:@"iPad6,7"]) return @"iPad Pro (12.9 inch) 1G (Wi-Fi)"; // http://pdadb.net/index.php?m=specs&id=8960&c=apple_ipad_pro_wifi_a1584_128gb
if ([modelIdentifier isEqualToString:@"iPad6,8"]) return @"iPad Pro (12.9 inch) 1G (Cellular)"; // http://pdadb.net/index.php?m=specs&id=8965&c=apple_ipad_pro_td-lte_a1652_32gb_apple_ipad_6,8
if ([modelIdentifier isEqualToString:@"iPad 7,1"]) return @"iPad Pro (12.9 inch) 2G (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad 7,2"]) return @"iPad Pro (12.9 inch) 2G (Cellular)";
if ([modelIdentifier isEqualToString:@"iPad 7,3"]) return @"iPad Pro (10.5 inch) 1G (Wi-Fi)";
if ([modelIdentifier isEqualToString:@"iPad 7,4"]) return @"iPad Pro (10.5 inch) 1G (Cellular)";
// iPod http://theiphonewiki.com/wiki/IPod
if ([modelIdentifier isEqualToString:@"iPod1,1"]) return @"iPod touch 1G";
if ([modelIdentifier isEqualToString:@"iPod2,1"]) return @"iPod touch 2G";
if ([modelIdentifier isEqualToString:@"iPod3,1"]) return @"iPod touch 3G";
if ([modelIdentifier isEqualToString:@"iPod4,1"]) return @"iPod touch 4G";
if ([modelIdentifier isEqualToString:@"iPod5,1"]) return @"iPod touch 5G";
if ([modelIdentifier isEqualToString:@"iPod7,1"]) return @"iPod touch 6G"; // as 6,1 was never released 7,1 is actually 6th generation
// Apple TV https://www.theiphonewiki.com/wiki/Apple_TV
if ([modelIdentifier isEqualToString:@"AppleTV1,1"]) return @"Apple TV 1G";
if ([modelIdentifier isEqualToString:@"AppleTV2,1"]) return @"Apple TV 2G";
if ([modelIdentifier isEqualToString:@"AppleTV3,1"]) return @"Apple TV 3G";
if ([modelIdentifier isEqualToString:@"AppleTV3,2"]) return @"Apple TV 3G"; // small, incremental update over 3,1
if ([modelIdentifier isEqualToString:@"AppleTV5,3"]) return @"Apple TV 4G"; // as 4,1 was never released, 5,1 is actually 4th generation
// Simulator
if ([modelIdentifier hasSuffix:@"86"] || [modelIdentifier isEqual:@"x86_64"])
{
BOOL smallerScreen = ([[UIScreen mainScreen] bounds].size.width < 768.0);
return (smallerScreen ? @"iPhone Simulator" : @"iPad Simulator");
}
return modelIdentifier;
}
@end