ios硬件采集信息汇总

来源:互联网 发布:苹果6s无法加入网络 编辑:程序博客网 时间:2024/06/04 20:05

年前,一直在做IOS前端数据采集这一块。。所以就整理了下,这些用到的东西...后继有可能还有补充

1.CPU类型获取

需要引入以下头文件,CPU类型放在 mach/machine.h中

#include <sys/types.h>#include <sys/sysctl.h>#include <mach/machine.h>

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. +(NSString*)getCPUType  
  2. {  
  3.     NSMutableString *cpu = [[NSMutableString alloc] init];  
  4.     size_t size;  
  5.     cpu_type_t type;  
  6.     cpu_subtype_t subtype;  
  7.     size = sizeof(type);  
  8.     sysctlbyname("hw.cputype", &type, &size, NULL0);  
  9.       
  10.     size = sizeof(subtype);  
  11.     sysctlbyname("hw.cpusubtype", &subtype, &size, NULL0);  
  12.       
  13.     // values for cputype and cpusubtype defined in mach/machine.h  
  14.     if (type == CPU_TYPE_X86)  
  15.     {  
  16.         [cpu appendString:@"x86 "];  
  17.         // check for subtype ...  
  18.           
  19.     } else if (type == CPU_TYPE_ARM)  
  20.     {  
  21.         [cpu appendString:@"ARM"];  
  22.         [cpu appendFormat:@",Type:%d",subtype];  
  23.     }  
  24.     return [cpu autorelease];  
  25.   
  26. }  


2.获取设备总内存

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. + (NSUInteger)getTotalMemoryBytes  
  2. {  
  3.     size_t size = sizeof(int);  
  4.     int results;  
  5.     int mib[2] = {CTL_HW, HW_PHYSMEM};  
  6.     sysctl(mib, 2, &results, &size, NULL0);  
  7.     return (NSUInteger) results/1024/1024;  
  8. }  


3.获取当前应用所占得内存

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. +(double)<span style="font-family: Menlo; font-size: 11px;">getCurrentApplicationUseMemory</span>  
  2. {  
  3.     task_basic_info_data_t taskInfo;  
  4.     mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;  
  5.     kern_return_t kernReturn = task_info(mach_task_self(),  
  6.                                          TASK_BASIC_INFO,  
  7.                                          (task_info_t)&taskInfo,  
  8.                                          &infoCount);  
  9.       
  10.     if (kernReturn != KERN_SUCCESS  
  11.         ) {  
  12.         return NSNotFound;  
  13.     }  
  14.       
  15.     return taskInfo.resident_size / 1024.0 / 1024.0;  
  16. }  



4.获取MMC(国家)MNC(运营商)    对应码列表  http://en.wikipedia.org/wiki/Mobile_country_code#C

需要引入 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import <CoreTelephony/CTTelephonyNetworkInfo.h>  
  2. #import <CoreTelephony/CTCarrier.h>  

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. +(NSDictionary*)getMCCAndMNCInfo  
  2. {  
  3.     CTTelephonyNetworkInfo* ctt=[[[CTTelephonyNetworkInfo alloc]init]autorelease];  
  4.     return [NSDictionary dictionaryWithObjectsAndKeys:ctt.subscriberCellularProvider.mobileNetworkCode,@"MNC",  
  5.             ctt.subscriberCellularProvider.mobileCountryCode,@"MCC", nil nil];  
  6. }  



4.异常收集处理(可以发送网络请求,这里就直接写调用EMAIL了)

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void UncaughtExceptionHandler(NSException *exception) {  
  2.     NSArray *arr = [exception callStackSymbols];  
  3.     NSString *reason = [exception reason];  
  4.     NSString *name = [exception name];  
  5.     NSString *urlStr = [NSString stringWithFormat:@"mailto://%@?subject=bug报告&body=感谢您的配合!<br><br><br>"  
  6.                         "错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",  
  7.                         _email,name,reason,[arr componentsJoinedByString:@"<br>"]];  
  8.     NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  
  9.     [[UIApplication sharedApplication] openURL:url];  
  10. }  

调用方法
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)writeACrashMessage  
  2. {  
  3.     NSSetUncaughtExceptionHandler(&C的函数名);  
  4. }  


5.获取运行中的进程

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. + (NSArray *)getRunningProcesses {  
  2.       
  3.     int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};  
  4.     size_t miblen = 4;  
  5.       
  6.     size_t size;  
  7.     int st = sysctl(mib, miblen, NULL, &size, NULL0);  
  8.       
  9.     struct kinfo_proc * process = NULL;  
  10.     struct kinfo_proc * newprocess = NULL;  
  11.       
  12.     do {  
  13.           
  14.         size += size / 10;  
  15.         newprocess = realloc(process, size);  
  16.           
  17.         if (!newprocess){  
  18.               
  19.             if (process){  
  20.                 free(process);  
  21.             }  
  22.               
  23.             return nil;  
  24.         }  
  25.           
  26.         process = newprocess;  
  27.         st = sysctl(mib, miblen, process, &size, NULL0);  
  28.           
  29.     } while (st == -1 && errno == ENOMEM);  
  30.       
  31.     if (st == 0){  
  32.           
  33.         if (size % sizeof(struct kinfo_proc) == 0){  
  34.             int nprocess = size / sizeof(struct kinfo_proc);  
  35.               
  36.             if (nprocess){  
  37.                   
  38.                 NSMutableArray * array = [[NSMutableArray alloc] init];  
  39.                   
  40.                 for (int i = nprocess - 1; i >= 0; i--){  
  41.                       
  42.                     NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];  
  43.                     NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];  
  44.                       
  45.                     NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil nil]  
  46.                                                                         forKeys:[NSArray arrayWithObjects:@"ProcessID"@"ProcessName", nil nil]];  
  47.                     [processID release];  
  48.                     [processName release];  
  49.                     [array addObject:dict];  
  50.                     [dict release];  
  51.                 }  
  52.                   
  53.                 free(process);  
  54.                 return [array autorelease];  
  55.             }  
  56.         }  
  57.     }  
  58.       
  59.       
  60.     return nil;  
  61. }  


6.IOS获取 网络类型(需要IOS7以后的版本)

需要引入以下框架

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import <CoreTelephony/CTTelephonyNetworkInfo.h>  

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyGPRS          __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  3.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyEdge          __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  4.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyWCDMA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  5.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSDPA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  6.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyHSUPA         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  7.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMA1x        __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  8.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORev0  __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  9.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevA  __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  10.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyCDMAEVDORevB  __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  11.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyeHRPD         __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  12.  CORETELEPHONY_EXTERN NSString * const CTRadioAccessTechnologyLTE           __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0); 
  13.  **/  

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. +(NSString*)getNetworkType  
  2. {  
  3.     CTTelephonyNetworkInfo* info=[[[CTTelephonyNetworkInfo alloc]init]autorelease];  
  4.     return info.currentRadioAccessTechnology;  
0 0