348 lines
12 KiB
Mathematica
348 lines
12 KiB
Mathematica
|
|
|
||
|
|
#pragma clang diagnostic push
|
||
|
|
#pragma clang diagnostic ignored "-Wbuiltin-macro-redefined"
|
||
|
|
#define __FILE__ "UIDevice+hw"
|
||
|
|
#pragma clang diagnostic pop
|
||
|
|
|
||
|
|
//
|
||
|
|
// UIDevice+UIDeviceEX.m
|
||
|
|
// QQMSFContact
|
||
|
|
//
|
||
|
|
// Created by zheng bingchao on 13-8-17.
|
||
|
|
//
|
||
|
|
//
|
||
|
|
|
||
|
|
#import "UIDevice+hw.h"
|
||
|
|
#include <sys/socket.h>
|
||
|
|
#include <sys/sysctl.h>
|
||
|
|
#include <net/if.h>
|
||
|
|
#include <net/if_dl.h>
|
||
|
|
#import <ifaddrs.h>
|
||
|
|
#import <arpa/inet.h>
|
||
|
|
|
||
|
|
@implementation UIDevice (Hardware)
|
||
|
|
/*
|
||
|
|
Platforms
|
||
|
|
|
||
|
|
iFPGA -> ??
|
||
|
|
|
||
|
|
iPhone1,1 -> iPhone 1G, M68
|
||
|
|
iPhone1,2 -> iPhone 3G, N82
|
||
|
|
iPhone2,1 -> iPhone 3GS, N88
|
||
|
|
iPhone3,1 -> iPhone 4/AT&T, N89
|
||
|
|
iPhone3,2 -> iPhone 4/Other Carrier?, ??
|
||
|
|
iPhone3,3 -> iPhone 4/Verizon, TBD
|
||
|
|
iPhone4,1 -> (iPhone 4S/GSM), TBD
|
||
|
|
iPhone4,2 -> (iPhone 4S/CDMA), TBD
|
||
|
|
iPhone4,3 -> (iPhone 4S/???)
|
||
|
|
iPhone5,1 -> iPhone Next Gen, TBD
|
||
|
|
iPhone5,1 -> iPhone Next Gen, TBD
|
||
|
|
iPhone5,1 -> iPhone Next Gen, TBD
|
||
|
|
|
||
|
|
iPod1,1 -> iPod touch 1G, N45
|
||
|
|
iPod2,1 -> iPod touch 2G, N72
|
||
|
|
iPod2,2 -> Unknown, ??
|
||
|
|
iPod3,1 -> iPod touch 3G, N18
|
||
|
|
iPod4,1 -> iPod touch 4G, N80
|
||
|
|
|
||
|
|
// Thanks NSForge
|
||
|
|
iPad1,1 -> iPad 1G, WiFi and 3G, K48
|
||
|
|
iPad2,1 -> iPad 2G, WiFi, K93
|
||
|
|
iPad2,2 -> iPad 2G, GSM 3G, K94
|
||
|
|
iPad2,3 -> iPad 2G, CDMA 3G, K95
|
||
|
|
iPad3,1 -> (iPad 3G, WiFi)
|
||
|
|
iPad3,2 -> (iPad 3G, GSM)
|
||
|
|
iPad3,3 -> (iPad 3G, CDMA)
|
||
|
|
iPad4,1 -> (iPad 4G, WiFi)
|
||
|
|
iPad4,2 -> (iPad 4G, GSM)
|
||
|
|
iPad4,3 -> (iPad 4G, CDMA)
|
||
|
|
|
||
|
|
AppleTV2,1 -> AppleTV 2, K66
|
||
|
|
AppleTV3,1 -> AppleTV 3, ??
|
||
|
|
|
||
|
|
i386, x86_64 -> iPhone Simulator
|
||
|
|
*/
|
||
|
|
|
||
|
|
#pragma mark - Public
|
||
|
|
//设备平台
|
||
|
|
- (NSString *)platform
|
||
|
|
{
|
||
|
|
static NSString *model;
|
||
|
|
static dispatch_once_t onceToken;
|
||
|
|
|
||
|
|
dispatch_once(&onceToken, ^{
|
||
|
|
model = [self model];
|
||
|
|
});
|
||
|
|
|
||
|
|
return (model?:@"iPhoneUnknown");
|
||
|
|
}
|
||
|
|
|
||
|
|
//设备平台类型
|
||
|
|
- (NSUInteger)platformType
|
||
|
|
{
|
||
|
|
NSString *platform = [self platform];
|
||
|
|
|
||
|
|
// The ever mysterious iFPGA
|
||
|
|
if ([platform isEqualToString:@"iFPGA"]) return UIDeviceIFPGA;
|
||
|
|
|
||
|
|
// iPhone
|
||
|
|
if ([platform isEqualToString:@"iPhone1,1"]) return UIDevice1GiPhone;
|
||
|
|
if ([platform isEqualToString:@"iPhone1,2"]) return UIDevice3GiPhone;
|
||
|
|
if ([platform hasPrefix:@"iPhone2"]) return UIDevice3GSiPhone;
|
||
|
|
if ([platform hasPrefix:@"iPhone3"]) return UIDevice4iPhone;
|
||
|
|
if ([platform hasPrefix:@"iPhone4"]) return UIDevice4SiPhone;
|
||
|
|
if ([platform hasPrefix:@"iPhone5"]) return UIDevice5iPhone;
|
||
|
|
if ([platform isEqualToString:@"iPhone6,2"]
|
||
|
|
|| [platform isEqualToString:@"iPhone6,1"]) return UIDevice5SiPhone; //6,1电信 6,2移动联通
|
||
|
|
if ([platform isEqualToString:@"iPhone7,1"]) return UIDevice6Plus;
|
||
|
|
if ([platform isEqualToString:@"iPhone7,2"]) return UIDevice6;
|
||
|
|
if ([platform isEqualToString:@"iPhone8,1"]) return UIDevice6SiPhone;
|
||
|
|
if ([platform isEqualToString:@"iPhone8,2"]) return UIDevice6SPlus;
|
||
|
|
if ([platform isEqualToString:@"iPhone8,4"]) return UIDeviceSE;
|
||
|
|
if ([platform isEqualToString:@"iPhone9,1"] || [platform isEqualToString:@"iPhone9,3"]) return UIDevice7iPhone;
|
||
|
|
if ([platform isEqualToString:@"iPhone9,2"] || [platform isEqualToString:@"iPhone9,4"]) return UIDevice7Plus;
|
||
|
|
if ([platform isEqualToString:@"iPhone10,1"] || [platform isEqualToString:@"iPhone10,4"]) return UIDevice8iPhone;
|
||
|
|
if ([platform isEqualToString:@"iPhone10,2"] || [platform isEqualToString:@"iPhone10,5"]) return UIDevice8Plus;
|
||
|
|
if ([platform isEqualToString:@"iPhone10,3"] || [platform isEqualToString:@"iPhone10,6"]) return UIDeviceXiPhone;
|
||
|
|
|
||
|
|
// iPod
|
||
|
|
if ([platform hasPrefix:@"iPod1"]) return UIDevice1GiPod;
|
||
|
|
if ([platform hasPrefix:@"iPod2"]) return UIDevice2GiPod;
|
||
|
|
if ([platform hasPrefix:@"iPod3"]) return UIDevice3GiPod;
|
||
|
|
if ([platform hasPrefix:@"iPod4"]) return UIDevice4GiPod;
|
||
|
|
if ([platform hasPrefix:@"iPod5,1"]) return UIDevice5GiPod;
|
||
|
|
|
||
|
|
// iPad
|
||
|
|
if ([platform hasPrefix:@"iPad1"]) return UIDevice1GiPad;
|
||
|
|
if ([platform hasPrefix:@"iPad2"]) return UIDevice2GiPad;
|
||
|
|
if ([platform hasPrefix:@"iPad3"]) return UIDevice3GiPad;
|
||
|
|
if ([platform hasPrefix:@"iPad4"]) return UIDevice4GiPad;
|
||
|
|
|
||
|
|
// Apple TV
|
||
|
|
if ([platform hasPrefix:@"AppleTV2"]) return UIDeviceAppleTV2;
|
||
|
|
if ([platform hasPrefix:@"AppleTV3"]) return UIDeviceAppleTV3;
|
||
|
|
|
||
|
|
if ([platform hasPrefix:@"iPhone"]) return UIDeviceUnknowniPhone;
|
||
|
|
if ([platform hasPrefix:@"iPod"]) return UIDeviceUnknowniPod;
|
||
|
|
if ([platform hasPrefix:@"iPad"]) return UIDeviceUnknowniPad;
|
||
|
|
if ([platform hasPrefix:@"AppleTV"]) return UIDeviceUnknownAppleTV;
|
||
|
|
|
||
|
|
// Simulator thanks Jordan Breeding
|
||
|
|
if ([platform hasSuffix:@"86"] || [platform isEqual:@"x86_64"])
|
||
|
|
{
|
||
|
|
if ((375 == [UIScreen mainScreen].bounds.size.width) && (812 == [UIScreen mainScreen].bounds.size.height)) {
|
||
|
|
return UIDeviceXiPhone;
|
||
|
|
}
|
||
|
|
BOOL smallerScreen = [UIScreen mainScreen].bounds.size.height < 768;
|
||
|
|
return smallerScreen ? UIDeviceSimulatoriPhone : UIDeviceSimulatoriPad;
|
||
|
|
}
|
||
|
|
|
||
|
|
return UIDeviceUnknown;
|
||
|
|
}
|
||
|
|
|
||
|
|
//设备平台字符串
|
||
|
|
- (NSString *)platformString
|
||
|
|
{
|
||
|
|
switch ([self platformType])
|
||
|
|
{
|
||
|
|
case UIDevice1GiPhone: return IPHONE_1G_NAMESTRING;
|
||
|
|
case UIDevice3GiPhone: return IPHONE_3G_NAMESTRING;
|
||
|
|
case UIDevice3GSiPhone: return IPHONE_3GS_NAMESTRING;
|
||
|
|
case UIDevice4iPhone: return IPHONE_4_NAMESTRING;
|
||
|
|
case UIDevice4SiPhone: return IPHONE_4S_NAMESTRING;
|
||
|
|
case UIDevice5iPhone: return IPHONE_5_NAMESTRING;
|
||
|
|
case UIDevice5SiPhone: return IPHONE_5S_NAMESTRING;
|
||
|
|
case UIDevice6: return IPHONE_6_NAMESTRING;
|
||
|
|
case UIDevice6Plus: return IPHONE_6PLUS_NAMESTRING;
|
||
|
|
case UIDevice6SiPhone: return IPHONE_6S_NAMESTRING;
|
||
|
|
case UIDevice6SPlus: return IPHONE_6SPlus_NAMESTRING;
|
||
|
|
case UIDevice7iPhone: return IPHONE_7_NAMESTRING;
|
||
|
|
case UIDevice7Plus: return IPHONE_7Plus_NAMESTRING;
|
||
|
|
case UIDevice8iPhone: return IPHONE_8_NAMESTRING;
|
||
|
|
case UIDevice8Plus: return IPHONE_8Plus_NAMESTRING;
|
||
|
|
case UIDeviceXiPhone: return IPHONE_X_NAMESTRING;
|
||
|
|
case UIDeviceUnknowniPhone: return IPHONE_UNKNOWN_NAMESTRING;
|
||
|
|
|
||
|
|
case UIDevice1GiPod: return IPOD_1G_NAMESTRING;
|
||
|
|
case UIDevice2GiPod: return IPOD_2G_NAMESTRING;
|
||
|
|
case UIDevice3GiPod: return IPOD_3G_NAMESTRING;
|
||
|
|
case UIDevice4GiPod: return IPOD_4G_NAMESTRING;
|
||
|
|
case UIDevice5GiPod: return IPOD_5G_NAMESTRING;
|
||
|
|
case UIDeviceUnknowniPod: return IPOD_UNKNOWN_NAMESTRING;
|
||
|
|
|
||
|
|
case UIDevice1GiPad : return IPAD_1G_NAMESTRING;
|
||
|
|
case UIDevice2GiPad : return IPAD_2G_NAMESTRING;
|
||
|
|
case UIDevice3GiPad : return IPAD_3G_NAMESTRING;
|
||
|
|
case UIDevice4GiPad : return IPAD_4G_NAMESTRING;
|
||
|
|
case UIDeviceUnknowniPad : return IPAD_UNKNOWN_NAMESTRING;
|
||
|
|
|
||
|
|
case UIDeviceAppleTV2 : return APPLETV_2G_NAMESTRING;
|
||
|
|
case UIDeviceAppleTV3 : return APPLETV_3G_NAMESTRING;
|
||
|
|
case UIDeviceAppleTV4 : return APPLETV_4G_NAMESTRING;
|
||
|
|
case UIDeviceUnknownAppleTV: return APPLETV_UNKNOWN_NAMESTRING;
|
||
|
|
|
||
|
|
case UIDeviceSimulator: return SIMULATOR_NAMESTRING;
|
||
|
|
case UIDeviceSimulatoriPhone: return SIMULATOR_IPHONE_NAMESTRING;
|
||
|
|
case UIDeviceSimulatoriPad: return SIMULATOR_IPAD_NAMESTRING;
|
||
|
|
case UIDeviceSimulatorAppleTV: return SIMULATOR_APPLETV_NAMESTRING;
|
||
|
|
|
||
|
|
case UIDeviceIFPGA: return IFPGA_NAMESTRING;
|
||
|
|
|
||
|
|
default: return IOS_FAMILY_UNKNOWN_DEVICE;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//设备所属平台家族
|
||
|
|
- (UIDeviceFamily)deviceFamily
|
||
|
|
{
|
||
|
|
NSString *platform = [self platformString];
|
||
|
|
if ([platform hasPrefix:@"iPhone"]) return UIDeviceFamilyiPhone;
|
||
|
|
if ([platform hasPrefix:@"iPod"]) return UIDeviceFamilyiPod;
|
||
|
|
if ([platform hasPrefix:@"iPad"]) return UIDeviceFamilyiPad;
|
||
|
|
if ([platform hasPrefix:@"AppleTV"]) return UIDeviceFamilyAppleTV;
|
||
|
|
|
||
|
|
return UIDeviceFamilyUnknown;
|
||
|
|
}
|
||
|
|
|
||
|
|
//设备总内存
|
||
|
|
- (NSUInteger)totalMemory
|
||
|
|
{
|
||
|
|
return [self getSysInfo:HW_PHYSMEM];
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
extern NSString *NSFileSystemSize;
|
||
|
|
extern NSString *NSFileSystemFreeSize;
|
||
|
|
extern NSString *NSFileSystemNodes;
|
||
|
|
extern NSString *NSFileSystemFreeNodes;
|
||
|
|
extern NSString *NSFileSystemNumber;
|
||
|
|
*/
|
||
|
|
//设备磁盘总大小
|
||
|
|
- (NSNumber *)totalDiskSpace {
|
||
|
|
NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
|
||
|
|
return fattributes[NSFileSystemSize];
|
||
|
|
}
|
||
|
|
|
||
|
|
//设备磁盘空闲大小
|
||
|
|
- (NSNumber *)freeDiskSpace
|
||
|
|
{
|
||
|
|
NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
|
||
|
|
return fattributes[NSFileSystemFreeSize];
|
||
|
|
}
|
||
|
|
|
||
|
|
//设备MAC地址
|
||
|
|
- (NSString *)macaddress
|
||
|
|
{
|
||
|
|
int mib[6];
|
||
|
|
size_t len;
|
||
|
|
char *buf;
|
||
|
|
unsigned char *ptr;
|
||
|
|
struct if_msghdr *ifm;
|
||
|
|
struct sockaddr_dl *sdl;
|
||
|
|
|
||
|
|
mib[0] = CTL_NET;
|
||
|
|
mib[1] = AF_ROUTE;
|
||
|
|
mib[2] = 0;
|
||
|
|
mib[3] = AF_LINK;
|
||
|
|
mib[4] = NET_RT_IFLIST;
|
||
|
|
|
||
|
|
if ((mib[5] = if_nametoindex("en0")) == 0) {
|
||
|
|
printf("Error: if_nametoindex error\n");
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
|
||
|
|
printf("Error: sysctl, take 1\n");
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ((buf = malloc(len)) == NULL) {
|
||
|
|
printf("Error: Memory allocation error\n");
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
|
||
|
|
printf("Error: sysctl, take 2\n");
|
||
|
|
free(buf); // Thanks, Remy "Psy" Demerest
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
ifm = (struct if_msghdr *)buf;
|
||
|
|
sdl = (struct sockaddr_dl *)(ifm + 1);
|
||
|
|
ptr = (unsigned char *)LLADDR(sdl);
|
||
|
|
NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
|
||
|
|
|
||
|
|
free(buf);
|
||
|
|
return outstring;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
- (NSString *)currentIPAddress
|
||
|
|
{
|
||
|
|
struct ifaddrs *interfaces = NULL;
|
||
|
|
struct ifaddrs *temp_addr = NULL;
|
||
|
|
NSString *wifiAddress = nil;
|
||
|
|
NSString *cellAddress = nil;
|
||
|
|
|
||
|
|
// retrieve the current interfaces - returns 0 on success
|
||
|
|
if(!getifaddrs(&interfaces)) {
|
||
|
|
// Loop through linked list of interfaces
|
||
|
|
temp_addr = interfaces;
|
||
|
|
while(temp_addr != NULL) {
|
||
|
|
sa_family_t sa_type = temp_addr->ifa_addr->sa_family;
|
||
|
|
if(sa_type == AF_INET || sa_type == AF_INET6) {
|
||
|
|
NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];
|
||
|
|
NSString *addr = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; // pdp_ip0
|
||
|
|
//NSLog(@"NAME: \"%@\" addr: %@", name, addr); // see for yourself
|
||
|
|
|
||
|
|
if([name isEqualToString:[NSString stringWithCString:"en0" encoding:NSUTF8StringEncoding]])
|
||
|
|
// Interface is the wifi connection on the iPhone
|
||
|
|
wifiAddress = addr;
|
||
|
|
else
|
||
|
|
if([name isEqualToString:[NSString stringWithCString:"pdp_ip0" encoding:NSUTF8StringEncoding]])
|
||
|
|
// Interface is the cell connection on the iPhone
|
||
|
|
cellAddress = addr;
|
||
|
|
|
||
|
|
}
|
||
|
|
temp_addr = temp_addr->ifa_next;
|
||
|
|
}
|
||
|
|
// Free memory
|
||
|
|
freeifaddrs(interfaces);
|
||
|
|
}
|
||
|
|
NSString *addr = wifiAddress ? wifiAddress : cellAddress;
|
||
|
|
return addr ? addr : @"0.0.0.0";
|
||
|
|
}
|
||
|
|
|
||
|
|
#pragma mark sysctl utils
|
||
|
|
- (NSUInteger)getSysInfo:(uint)typeSpecifier
|
||
|
|
{
|
||
|
|
size_t size = sizeof(int);
|
||
|
|
unsigned long long results;
|
||
|
|
int mib[2] = {CTL_HW, typeSpecifier};
|
||
|
|
sysctl(mib, 2, &results, &size, NULL, 0);
|
||
|
|
return (NSUInteger) results;
|
||
|
|
}
|
||
|
|
|
||
|
|
#pragma mark platform type and name utils
|
||
|
|
- (NSString *)model {
|
||
|
|
static NSString *platform;
|
||
|
|
static dispatch_once_t onceToken;
|
||
|
|
|
||
|
|
dispatch_once(&onceToken, ^{
|
||
|
|
size_t size;
|
||
|
|
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
|
||
|
|
char *machine = (char *)malloc(size);
|
||
|
|
sysctlbyname("hw.machine", machine, &size, NULL, 0);
|
||
|
|
if (machine == NULL) {
|
||
|
|
platform = @"i386";
|
||
|
|
} else {
|
||
|
|
platform = [NSString stringWithUTF8String:machine];
|
||
|
|
}
|
||
|
|
free(machine);
|
||
|
|
});
|
||
|
|
|
||
|
|
return (platform?:@"");
|
||
|
|
}
|
||
|
|
|
||
|
|
@end
|