IOS硬件信息采集汇总
2014-11-10 11:15
218 查看
年前,一直在做IOS前端数据采集这一块。。所以就整理了下,这些用到的东西...后继有可能还有补充
1.CPU类型获取
需要引入以下头文件,CPU类型放在 mach/machine.h中
[objc] view
plaincopy
+(NSString*)getCPUType
{
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 autorelease];
}
2.获取设备总内存
[objc] view
plaincopy
+ (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;
}
3.获取当前应用所占得内存
[objc] view
plaincopy
+(double)<span style="font-family: Menlo; font-size: 11px;">getCurrentApplicationUseMemory</span>
{
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&taskInfo,
&infoCount);
if (kernReturn != KERN_SUCCESS
) {
return NSNotFound;
}
return taskInfo.resident_size / 1024.0 / 1024.0;
}
4.获取MMC(国家)MNC(运营商) 对应码列表 http://en.wikipedia.org/wiki/Mobile_country_code#C
需要引入
[objc] view
plaincopy
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
[objc] view
plaincopy
+(NSDictionary*)getMCCAndMNCInfo
{
CTTelephonyNetworkInfo* ctt=[[[CTTelephonyNetworkInfo alloc]init]autorelease];
return [NSDictionary dictionaryWithObjectsAndKeys:ctt.subscriberCellularProvider.mobileNetworkCode,@"MNC",
ctt.subscriberCellularProvider.mobileCountryCode,@"MCC", nil nil];
}
4.异常收集处理(可以发送网络请求,这里就直接写调用EMAIL了)
[objc] view
plaincopy
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
NSString *urlStr = [NSString stringWithFormat:@"mailto://%@?subject=bug报告&body=感谢您的配合!<br><br><br>"
"错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",
_email,name,reason,[arr componentsJoinedByString:@"<br>"]];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
调用方法
[objc] view
plaincopy
-(void)writeACrashMessage
{
NSSetUncaughtExceptionHandler(&C的函数名);
}
5.获取运行中的进程
[objc] view
plaincopy
+ (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 nil]
forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil nil]];
[processID release];
[processName release];
[array addObject:dict];
[dict release];
}
free(process);
return [array autorelease];
}
}
}
return nil;
}
6.IOS获取 网络类型(需要IOS7以后的版本)
需要引入以下框架
[objc] view
plaincopy
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
[objc] view
plaincopy
/**
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);
**/
[objc] view
plaincopy
+(NSString*)getNetworkType
{
CTTelephonyNetworkInfo* info=[[[CTTelephonyNetworkInfo alloc]init]autorelease];
return info.currentRadioAccessTechnology;
1.CPU类型获取
需要引入以下头文件,CPU类型放在 mach/machine.h中
#include <sys/types.h> #include <sys/sysctl.h> #include <mach/machine.h>
[objc] view
plaincopy
+(NSString*)getCPUType
{
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 autorelease];
}
2.获取设备总内存
[objc] view
plaincopy
+ (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;
}
3.获取当前应用所占得内存
[objc] view
plaincopy
+(double)<span style="font-family: Menlo; font-size: 11px;">getCurrentApplicationUseMemory</span>
{
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&taskInfo,
&infoCount);
if (kernReturn != KERN_SUCCESS
) {
return NSNotFound;
}
return taskInfo.resident_size / 1024.0 / 1024.0;
}
4.获取MMC(国家)MNC(运营商) 对应码列表 http://en.wikipedia.org/wiki/Mobile_country_code#C
需要引入
[objc] view
plaincopy
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
[objc] view
plaincopy
+(NSDictionary*)getMCCAndMNCInfo
{
CTTelephonyNetworkInfo* ctt=[[[CTTelephonyNetworkInfo alloc]init]autorelease];
return [NSDictionary dictionaryWithObjectsAndKeys:ctt.subscriberCellularProvider.mobileNetworkCode,@"MNC",
ctt.subscriberCellularProvider.mobileCountryCode,@"MCC", nil nil];
}
4.异常收集处理(可以发送网络请求,这里就直接写调用EMAIL了)
[objc] view
plaincopy
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
NSString *urlStr = [NSString stringWithFormat:@"mailto://%@?subject=bug报告&body=感谢您的配合!<br><br><br>"
"错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",
_email,name,reason,[arr componentsJoinedByString:@"<br>"]];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
调用方法
[objc] view
plaincopy
-(void)writeACrashMessage
{
NSSetUncaughtExceptionHandler(&C的函数名);
}
5.获取运行中的进程
[objc] view
plaincopy
+ (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 nil]
forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil nil]];
[processID release];
[processName release];
[array addObject:dict];
[dict release];
}
free(process);
return [array autorelease];
}
}
}
return nil;
}
6.IOS获取 网络类型(需要IOS7以后的版本)
需要引入以下框架
[objc] view
plaincopy
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
[objc] view
plaincopy
/**
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);
**/
[objc] view
plaincopy
+(NSString*)getNetworkType
{
CTTelephonyNetworkInfo* info=[[[CTTelephonyNetworkInfo alloc]init]autorelease];
return info.currentRadioAccessTechnology;
相关文章推荐
- IOS硬件信息采集汇总
- IOS硬件信息采集汇总
- ios硬件采集信息汇总
- IOS硬件信息采集汇总
- iOS开发 证书相关信息汇总
- ios获取硬件信息
- linux中硬件信息查询汇总
- iOS设备硬件规格完整汇总
- 汇总 Linux下获取详细硬件信息的工具:Dmidecode命令详解
- DELL服务器硬件信息采集SHELL脚本
- Linux硬件信息查询汇总一览
- 【信息采集】IBM AIX系统硬件信息查看命令
- DELL服务器硬件信息采集SHELL脚本
- linux 系统版本信息、磁盘信息、硬件信息显示汇总
- iOS如何获取设备硬件信息
- 本文作者对iOS开发者及设计师在面试时可能会遇到的问题进行了筛选与汇总。一方面,能够帮助HR在短时间内获取更多反馈信息,以甄选合适人选,而开发者及设计师在寻找相关工作时,也可作为参考,为面试做好万全准
- 关于iOS APP需要支持64位相关信息汇总
- 【信息采集】IBM AIX系统硬件信息查看命令(shell脚本)
- linux系统查看硬件以及系统信息常用命令汇总
- iOS设备硬件规格完整汇总