您的位置:首页 > 移动开发 > IOS开发

ios硬件采集信息汇总

2014-08-06 16:34 190 查看
年前,一直在做IOS前端数据采集这一块。。所以就整理了下,这些用到的东西...后继有可能还有补充

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 硬件信息采集