iOS获取硬件系统信息
一.获取系统相关信息
// 1.获取设备名称
+ (NSString *)getDeviceName
{
NSString *strName = [[UIDevice currentDevice] name];
NSLog(@"%@",strName);
return strName;
}```
* 2.获取手机名称
``` objc
// 获取手机名称
+ (NSString *)getIphoneName
{
NSString *phoneName = [[UIDevice currentDevice] model];
return phoneName;
}
+ (NSString *)getApp_version
{
NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];
NSLog(@"%@",strSysVersion);
return strSysVersion;
}
// iOS7之前获取ID的方式已经被弃用,这里用的是另一种方式,代码详见附件
+ (NSString *)getDeviceID
{
// NSString *identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
//
// return identifierForVendor;
NSString *gId = [UUID getUUID];
return gId;
}
+(NSString *)getUUID
{
NSString * strUUID = (NSString *)[KeyChainStore load:@"com.company.app.usernamepassword"];
//首次执行该方法时,uuid为空
if ([strUUID isEqualToString:@""] || !strUUID)
{
//生成一个uuid的方法
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
//将该uuid保存到keychain
[KeyChainStore save:KEY_USERNAME_PASSWORD data:strUUID];
}
return strUUID;
}
+ (NSString *)getDeviceModel
{
struct utsname systemInfo;
uname(&systemInfo);
NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSArray *modelArray = @[
@"i386", @"x86_64",
@"iPhone1,1",
@"iPhone1,2",
@"iPhone2,1",
@"iPhone3,1",
@"iPhone3,2",
@"iPhone3,3",
@"iPhone4,1",
@"iPhone5,1",
@"iPhone5,2",
@"iPhone5,3",
@"iPhone5,4",
@"iPhone6,1",
@"iPhone6,2",
@"iPhone7,2",
@"iPhone7,1",
@"iPhone8,1",
@"iPhone8,2",
@"iPod1,1",
@"iPod2,1",
@"iPod3,1",
@"iPod4,1",
@"iPod5,1",
@"iPad1,1",
@"iPad2,1",
@"iPad2,2",
@"iPad2,3",
@"iPad2,4",
@"iPad3,1",
@"iPad3,2",
@"iPad3,3",
@"iPad3,4",
@"iPad3,5",
@"iPad3,6",
@"iPad2,5",
@"iPad2,6",
@"iPad2,7",
];
NSArray *modelNameArray = @[
@"iPhone Simulator",
@"iPhone Simulator",
@"iPhone 2G",
@"iPhone 3G",
@"iPhone 3GS",
@"iPhone 4(GSM)",
@"iPhone 4(GSM Rev A)",
@"iPhone 4(CDMA)",
@"iPhone 4S",
@"iPhone 5(GSM)",
@"iPhone 5(GSM+CDMA)",
@"iPhone 5c(GSM)",
@"iPhone 5c(Global)",
@"iphone 5s(GSM)",
@"iphone 5s(Global)",
@"iPhone 6",
@"iPhone 6 Plus",
@"iPhone 6s",
@"iPhone 6s Plus",
@"iPod Touch 1G",
@"iPod Touch 2G",
@"iPod Touch 3G",
@"iPod Touch 4G",
@"iPod Touch 5G",
@"iPad",
@"iPad 2(WiFi)",
@"iPad 2(GSM)",
@"iPad 2(CDMA)",
@"iPad 2(WiFi + New Chip)",
@"iPad 3(WiFi)",
@"iPad 3(GSM+CDMA)",
@"iPad 3(GSM)",
@"iPad 4(WiFi)",
@"iPad 4(GSM)",
@"iPad 4(GSM+CDMA)",
@"iPad mini (WiFi)",
@"iPad mini (GSM)",
@"ipad mini (GSM+CDMA)"
];
NSInteger modelIndex = - 1;
NSString *modelNameString = nil;
modelIndex = [modelArray indexOfObject:deviceString];
if (modelIndex >= 0 && modelIndex < [modelNameArray count]) {
modelNameString = [modelNameArray objectAtIndex:modelIndex];
}
return modelNameString ? modelNameString : @"iOS";
}
二.获取硬件相关信息
+ (NSString *)getHardParam // 返回CPU类型
{
NSMutableString *cpu = [[NSMutableString alloc] init];
size_t size;
cpu_type_t type;
cpu_subtype_t subtype;
size = sizeof(type);
sysctlbyname("hw.cputype", &type, &size, NULL, 0);
size = sizeof(subtype);
sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);
// values for cputype and cpusubtype defined in mach/machine.h
if (type == CPU_TYPE_X86)
{
[cpu appendString:@"x86 "];
// check for subtype ...
} else if (type == CPU_TYPE_ARM)
{
[cpu appendString:@"ARM"];
[cpu appendFormat:@",Type:%d",subtype];
}
return cpu;
}
+ (NSString *)getTelephonyInfo // 获取运营商信息
{
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [info subscriberCellularProvider];
NSString *mCarrier = [NSString stringWithFormat:@"%@",[carrier carrierName]];
return mCarrier;
}
// 获得设备总内存
+ (NSUInteger)getTotalMemoryBytes
{
size_t size = sizeof(int);
int results;
int mib[2] = {CTL_HW, HW_PHYSMEM};
sysctl(mib, 2, &results, &size, NULL, 0);
return (NSUInteger) results/1024/1024;
}
// 异常收集处理
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
NSLog(@"%@,%@,%@",arr,reason,name);
}
// 写入异常信息
-(void)writeACrashMessage
{
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
}
+ (NSArray *)getRunningProcesses {
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t miblen = 4;
size_t size;
int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
struct kinfo_proc * process = NULL;
struct kinfo_proc * newprocess = NULL;
do {
size += size / 10;
newprocess = realloc(process, size);
if (!newprocess){
if (process){
free(process);
}
return nil;
}
process = newprocess;
st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM);
if (st == 0){
if (size % sizeof(struct kinfo_proc) == 0){
int nprocess = size / sizeof(struct kinfo_proc);
if (nprocess){
NSMutableArray * array = [[NSMutableArray alloc] init];
for (int i = nprocess - 1; i >= 0; i--){
NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]
forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];
[array addObject:dict];
}
free(process);
return array;
}
}
}
return nil;
}
// 获取网络类型
+(NSString*)getNetworkType
{
/**
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyGPRS __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyEdge __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyWCDMA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSDPA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSUPA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMA1x __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORev0 __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevA __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevB __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyeHRPD __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyLTE __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
**/
CTTelephonyNetworkInfo* info=[[CTTelephonyNetworkInfo alloc]init];
return info.currentRadioAccessTechnology;
}
第一次在简书写,有不正确的地方还请指正,下面附上代码地址:
GitHub链接