forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyDeviceInfo.m
148 lines (121 loc) · 3.75 KB
/
CountlyDeviceInfo.m
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Test.m
// Navirize
//
// Created by Hamdy on 8/13/15.
// Copyright (c) 2015 RizeInnoFZCO. All rights reserved.
//
#import "CountlyDeviceInfo.h"
#import "Countly_OpenUDID.h"
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
#import <UIKit/UIKit.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#if COUNTLY_PREFER_IDFA
#import <AdSupport/ASIdentifierManager.h>
#endif
#endif
#include <sys/types.h>
#include <sys/sysctl.h>
#import <mach/mach.h>
#import <mach/mach_host.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#include <libkern/OSAtomic.h>
#include <execinfo.h>
#import "HelperFunctions.h"
#pragma mark - CountlyDeviceInfo
@implementation CountlyDeviceInfo
+ (NSString *)udid
{
#if COUNTLY_PREFER_IDFA && (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR || COUNTLY_TARGET_WATCHKIT)
return ASIdentifierManager.sharedManager.advertisingIdentifier.UUIDString;
#else
return [Countly_OpenUDID value];
#endif
}
+ (NSString *)device
{
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
char *modelKey = "hw.machine";
#else
char *modelKey = "hw.model";
#endif
size_t size;
sysctlbyname(modelKey, NULL, &size, NULL, 0);
char *model = malloc(size);
sysctlbyname(modelKey, model, &size, NULL, 0);
NSString *modelString = @(model);
free(model);
return modelString;
}
+ (NSString *)osName
{
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
return @"iOS";
#else
return @"OS X";
#endif
}
+ (NSString *)osVersion
{
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
return [[UIDevice currentDevice] systemVersion];
#else
return [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"][@"ProductVersion"];
#endif
}
+ (NSString *)carrier
{
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
if (NSClassFromString(@"CTTelephonyNetworkInfo"))
{
CTTelephonyNetworkInfo *netinfo = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
return [carrier carrierName];
}
#endif
return nil;
}
+ (NSString *)resolution
{
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
CGRect bounds = UIScreen.mainScreen.bounds;
CGFloat scale = [UIScreen.mainScreen respondsToSelector:@selector(scale)] ? [UIScreen.mainScreen scale] : 1.f;
return [NSString stringWithFormat:@"%gx%g", bounds.size.width * scale, bounds.size.height * scale];
#else
NSRect screenRect = NSScreen.mainScreen.frame;
CGFloat scale = [NSScreen.mainScreen backingScaleFactor];
return [NSString stringWithFormat:@"%gx%g", screenRect.size.width * scale, screenRect.size.height * scale];
#endif
}
+ (NSString *)locale
{
return [[NSLocale currentLocale] localeIdentifier];
}
+ (NSString *)appVersion
{
NSString *result = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
if ([result length] == 0)
result = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey];
return result;
}
+ (NSString *)metrics
{
NSMutableDictionary* metricsDictionary = [NSMutableDictionary dictionary];
metricsDictionary[@"_device"] = CountlyDeviceInfo.device;
metricsDictionary[@"_os"] = CountlyDeviceInfo.osName;
metricsDictionary[@"_os_version"] = CountlyDeviceInfo.osVersion;
NSString *carrier = CountlyDeviceInfo.carrier;
if (carrier)
metricsDictionary[@"_carrier"] = carrier;
metricsDictionary[@"_resolution"] = CountlyDeviceInfo.resolution;
metricsDictionary[@"_locale"] = CountlyDeviceInfo.locale;
metricsDictionary[@"_app_version"] = CountlyDeviceInfo.appVersion;
return CountlyURLEscapedString(CountlyJSONFromObject(metricsDictionary));
}
+ (NSString *)bundleId
{
return [[NSBundle mainBundle] bundleIdentifier];
}
@end