iOS设备信息收集

收集iOS设备的信息,包含设备系统版本,设备型号,手机型号,屏幕分辨率,当地所在时区,CPU 型号,系统语言,网络环境,应用名称以及应用版本,还有判断设备是否,应用是否被破解

需要的头文件

1
2
3
4
5
6
7
8
9
10
11
#import "ISSMobileClick.h"
#import "AppInfoModel.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <objc/runtime.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/machine.h>
#include <sys/types.h>
#import "ITTNetworkTrafficManager.h"

实现方法,我使用类方法方便调用,无需实例化,直接类调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
+ (void)collectAppInfo
{
    //获取设备信息,如当前系统版本、设备型号,

    NSString *content=[[NSString alloc]
                       initWithFormat:
                       @"unique id: %@ \nlocalized model: %@ \nsystem version: %@ \nsystem name: %@ \nmodel: %@",
                       [[UIDevice currentDevice] identifierForVendor],
                       [[UIDevice currentDevice] localizedModel],
                       [[UIDevice currentDevice] systemVersion],
                       [[UIDevice currentDevice] systemName],
                       [[UIDevice currentDevice] model]];
    //platform手机型号
    //
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char*)malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];

    NSLog(@"手机型号 :%@",platform);
    //date
    NSString *date =[[NSDate date] stringWithFormat:@"yyyy-MM-dd"];
    NSString *time =[[NSDate date] stringWithFormat:@"HH:MM:SS"];

    NSLog(@"%@\napp version %@\ndate :%@\ntime:%@",content,XcodeAppVersion,date,time);

    CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc]init];
    CTCarrier*carrier = [netInfo subscriberCellularProvider];
    NSLog(@"Carriername:%@\nISO Country Code:%@",[carrier carrierName],[carrier isoCountryCode]);//手机运营商以及运营国家标号

    UIScreen *MainScreen = [UIScreen mainScreen];
    CGSize Size = [MainScreen bounds].size;
    CGFloat scale = [MainScreen scale];
    CGFloat screenWidth = Size.width * scale;
    CGFloat screenHeight = Size.height * scale;

    NSLog(@"屏幕分辨率为 :%.0f %.0f",screenWidth, screenHeight);

    NSLog(@"当地所在时区 :%@",[[NSTimeZone localTimeZone] description]);

    NSMutableString *cpu = [[NSMutableString alloc] init];

    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"];
        switch(subtype)
        {
            case CPU_SUBTYPE_ARM_V7:
                [cpu appendString:@"V7"];
                break;
            case CPU_SUBTYPE_ARM_V7S:
                [cpu appendString:@"V7s"];
                break;
                // ...
        }
    }

    NSLog(@"cpu 型号 :%@",cpu);

    NSArray *arLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
    NSString *strLang = [arLanguages objectAtIndex:0];
    NSLog(@"LANG:%@",strLang);

    NSLog(@"网络环境:%@",[ITTNetworkTrafficManager sharedManager].networkType);


    AppInfoModel *appInfo = [[AppInfoModel alloc] init];
    appInfo.uniqIdentifier = (NSString*)[[UIDevice currentDevice] identifierForVendor];
    appInfo.deviceModel = platform;
    appInfo.systemVersion = [[UIDevice currentDevice] systemVersion];
    appInfo.systemName = [[UIDevice currentDevice] systemName];
    appInfo.systemModel = [[UIDevice currentDevice] model];
    appInfo.currentDate = [NSDate date];
    appInfo.carrierName = [carrier carrierName];
    appInfo.carrierCountryCode = [carrier isoCountryCode];
    appInfo.screen = [NSString stringWithFormat:@"%.0fx%.0f",screenWidth, screenHeight];
    appInfo.timeZone = [[NSTimeZone localTimeZone] abbreviation];
    appInfo.sdkType = @"iOS";
    appInfo.packageName = PACKAGE_NAME;
    appInfo.appName = XcodeAppName;
    NSLog(@"应用名称 :%@\ngmt %@",appInfo.appName ,[[NSTimeZone localTimeZone] abbreviation]);

}

真机运行之后,log信息如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2014-03-21 17:40:13.224 AppCountDemo[2004:60b] 手机型号 :iPhone5,4
2014-03-21 17:40:13.230 AppCountDemo[2004:60b] unique id: <__NSConcreteUUID 0x17d827f0> 0E21F593-06EB-4D3B-8DE4-8CAF31AB413A
localized model: iPhone
system version: 7.1
system name: iPhone OS
model: iPhone
app version 1.0
date :2014-03-21
time:17:03:22
2014-03-21 17:40:13.240 AppCountDemo[2004:60b] Carriername:中国联通
ISO Country Code:cn
2014-03-21 17:40:13.242 AppCountDemo[2004:60b] 屏幕分辨率为 :640 1136
2014-03-21 17:40:13.244 AppCountDemo[2004:60b] 当地所在时区 :Local Time Zone (Asia/Shanghai (GMT+8) offset 28800)
2014-03-21 17:40:13.246 AppCountDemo[2004:60b] cpu 型号 :ARMV7s
2014-03-21 17:40:13.247 AppCountDemo[2004:60b] LANG:zh-Hans
2014-03-21 17:40:13.260 AppCountDemo[2004:60b] Reachability Flag Status: -R -----l- networkStatusForFlags
2014-03-21 17:40:13.261 AppCountDemo[2004:60b] -[ITTNetworkTrafficManager updateNetwordStatus:](120): network status wifi
2014-03-21 17:40:13.304 AppCountDemo[2004:60b] 网络环境:wifi
2014-03-21 17:40:13.306 AppCountDemo[2004:60b] 应用名称 :统计SDKDemo
gmt GMT+8

最后聊一下,目前遇到的问题

  • 生成静态库,另一个工程载入静态库时候出现一个问题,Catagory使用出现错误,调用方法会报错,运行时exception “selector not recognized”。
  • 这是由于 UNIX的静态库实现、linker和Objective-C的动态结构三者之间的问题引起的。通过修改工程配置文件,只要在build静态库时,加上linker flag “-ObjC”即可(在64位osx上和iOS程序上,这样做还不够),这个flag告诉linker将每个定义了class或者category的对象文件都载入静态库。

    但是最终程序还是会抛这个异常,这是因为linker的bug,对于64位osx程序和iOS程序,这个bug导致只包含category而不包含class的文件没法从静态库中加载。

    Comments