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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
//
// MapboxEvents.m
// MapboxEvents
//
// Dynamic detection of ASIdentifierManager from Mixpanel
// https://github.com/mixpanel/mixpanel-iphone/blob/master/LICENSE
//
// Created by Brad Leege on 3/5/15.
// Copyright (c) 2015 Mapbox. All rights reserved.
//
#import "MGLMapboxEvents.h"
#import <UIKit/UIKit.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#include <sys/sysctl.h>
#import <SystemConfiguration/CaptiveNetwork.h>
@interface MGLMapboxEvents()
@property (atomic) NSMutableArray *queue;
@property (atomic) NSString *instance;
@property (atomic) NSString *anonid;
@property (atomic) NSTimer *timer;
@property (atomic) NSString *userAgent;
@end
@implementation MGLMapboxEvents
static MGLMapboxEvents *sharedManager = nil;
NSDateFormatter *rfc3339DateFormatter = nil;
NSString *model;
NSString *iOSVersion;
NSString *carrier;
NSNumber *scale;
- (id) init {
self = [super init];
if (self) {
// Put Settings bundle into memory
NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
if(!settingsBundle) {
NSLog(@"Could not find Settings.bundle");
} else {
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
for(NSDictionary *prefSpecification in preferences) {
NSString *key = [prefSpecification objectForKey:@"Key"];
if(key && [[prefSpecification allKeys] containsObject:@"DefaultValue"]) {
[defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
}
}
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
}
// Configure Events Infrastructure
_queue = [[NSMutableArray alloc] init];
_flushAt = 20;
_flushAfter = 10000;
_api = @"https://api.tiles.mapbox.com";
_token = nil;
_instance = [[NSUUID UUID] UUIDString];
Class ASIdentifierManagerClass = NSClassFromString(@"ASIdentifierManager");
if (ASIdentifierManagerClass) {
SEL sharedManagerSelector = NSSelectorFromString(@"sharedManager");
id sharedManager = ((id (*)(id, SEL))[ASIdentifierManagerClass methodForSelector:sharedManagerSelector])(ASIdentifierManagerClass, sharedManagerSelector);
// Add check here
SEL isAdvertisingTrackingEnabledSelector = NSSelectorFromString(@"isAdvertisingTrackingEnabled");
BOOL trackingEnabled = ((BOOL (*)(id, SEL))[sharedManager methodForSelector:isAdvertisingTrackingEnabledSelector])(sharedManager, isAdvertisingTrackingEnabledSelector);
if (trackingEnabled) {
SEL advertisingIdentifierSelector = NSSelectorFromString(@"advertisingIdentifier");
NSUUID *uuid = ((NSUUID* (*)(id, SEL))[sharedManager methodForSelector:advertisingIdentifierSelector])(sharedManager, advertisingIdentifierSelector);
_anonid = [uuid UUIDString];
} else {
_anonid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
} else {
_anonid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
model = [self getSysInfoByName:"hw.machine"];
iOSVersion = [NSString stringWithFormat:@"%@ %@", [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion];
if ([UIScreen instancesRespondToSelector:@selector(nativeScale)]) {
scale = [[NSNumber alloc] initWithFloat:[UIScreen mainScreen].nativeScale];
} else {
scale = [[NSNumber alloc] initWithFloat:[UIScreen mainScreen].scale];
}
CTCarrier *carrierVendor = [[[CTTelephonyNetworkInfo alloc] init] subscriberCellularProvider];
carrier = [carrierVendor carrierName];
_userAgent = @"MapboxEventsiOS/1.0";
// Setup Date Format
rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
}
return self;
}
+ (id)sharedManager {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[self alloc] init];
});
return sharedManager;
}
- (void) pushEvent:(NSString *)event withAttributes:(NSDictionary *)attributeDictionary {
// TODO Add Opt Out Checking When Built
/**
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"mapbox_metrics_enabled_preference"]) {
NSLog(@"Mapbox Metrics are not enabled, so return without sending in data.");
return;
}
*/
// TODO - Add Metrics Disabled App Wide Check
if (!event) {
return;
}
NSMutableDictionary *evt = [[NSMutableDictionary alloc] init];
// mapbox-events stock attributes
[evt setObject:event forKey:@"event"];
[evt setObject:[NSNumber numberWithInt:1] forKey:@"version"];
[evt setObject:[self formatDate:[NSDate date]] forKey:@"created"];
[evt setObject:self.instance forKey:@"instance"];
[evt setObject:self.anonid forKey:@"anonid"];
// mapbox-events-ios stock attributes
[evt setValue:[rfc3339DateFormatter stringFromDate:[NSDate date]] forKey:@"deviceTimestamp"];
[evt setValue:model forKey:@"model"];
[evt setValue:iOSVersion forKey:@"operatingSystem"];
[evt setValue:[self getDeviceOrientation] forKey:@"orientation"];
[evt setValue:[[NSNumber alloc] initWithFloat:(100 * [UIDevice currentDevice].batteryLevel)] forKey:@"batteryLevel"];
[evt setValue:scale forKey:@"resolution"];
[evt setValue:carrier forKey:@"carrier"];
[evt setValue:[self getWifiNetworkName] forKey:@"wifi"];
[evt setValue:[NSNumber numberWithInt:[self getContentSizeScale]] forKey:@"accessibilityFontScale"];
for (NSString *key in [attributeDictionary allKeys]) {
[evt setObject:[attributeDictionary valueForKey:key] forKey:key];
}
// Make Immutable Version
NSDictionary *finalEvent = [NSDictionary dictionaryWithDictionary:evt];
// Put On The Queue
[self.queue addObject:finalEvent];
// Has Flush Limit Been Reached?
if ((int)_queue.count >= (int)_flushAt) {
[self flush];
}
// Reset Timer (Initial Starting of Timer after first event is pushed)
[self startTimer];
}
- (void) flush {
if (_token == nil) {
NSLog(@"token hasn't been set yet, so no events can be sent. return");
return;
}
int upper = (int)_flushAt;
if (_flushAt > [_queue count]) {
if ([_queue count] == 0) {
return;
}
upper = (int)[_queue count];
}
// Create Array of Events to push to the Server
NSRange theRange = NSMakeRange(0, upper);
NSArray *events = [_queue subarrayWithRange:theRange];
// Update Queue to remove events sent to server
[_queue removeObjectsInRange:theRange];
// Send Array of Events to Server
[self postEvents:events];
}
- (void) postEvents:(NSArray *)events {
// Setup URL Request
NSString *url = [NSString stringWithFormat:@"%@/events/v1?access_token=%@", _api, _token];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setValue:[self getUserAgent] forHTTPHeaderField:@"User-Agent"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
// Convert Array of Dictionaries to JSON
if ([NSJSONSerialization isValidJSONObject:events]) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:events options:NSJSONWritingPrettyPrinted error:nil];
[request setHTTPBody:jsonData];
// Send non blocking HTTP Request to server
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:nil];
}
}
- (void) startTimer {
// Stop Timer if it already exists
if (_timer) {
[_timer invalidate];
_timer = nil;
}
// Start New Timer
NSTimeInterval interval = (double)((NSInteger)_flushAfter);
_timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(flush) userInfo:nil repeats:YES];
}
- (NSString *) getUserAgent {
if (_appName != nil && _appVersion != nil && ([_userAgent rangeOfString:_appName].location == NSNotFound)) {
_userAgent = [NSString stringWithFormat:@"%@/%@ %@", _appName, _appVersion, _userAgent];
}
return _userAgent;
}
- (NSString *) formatDate:(NSDate *)date {
return [rfc3339DateFormatter stringFromDate:date];
}
- (NSString *) getDeviceOrientation {
switch ([UIDevice currentDevice].orientation) {
case UIDeviceOrientationUnknown:
return @"Unknown";
break;
case UIDeviceOrientationPortrait:
return @"Portrait";
break;
case UIDeviceOrientationPortraitUpsideDown:
return @"PortraitUpsideDown";
break;
case UIDeviceOrientationLandscapeLeft:
return @"LandscapeLeft";
break;
case UIDeviceOrientationLandscapeRight:
return @"LandscapeRight";
break;
case UIDeviceOrientationFaceUp:
return @"FaceUp";
break;
case UIDeviceOrientationFaceDown:
return @"FaceDown";
break;
default:
return @"Default - Unknown";
break;
}
}
- (int) getContentSizeScale {
NSString *sc = [UIApplication sharedApplication].preferredContentSizeCategory;
if ([sc isEqualToString:UIContentSizeCategoryExtraSmall]) {
return -3;
} else if ([sc isEqualToString:UIContentSizeCategorySmall]) {
return -2;
} else if ([sc isEqualToString:UIContentSizeCategoryMedium]) {
return -1;
} else if ([sc isEqualToString:UIContentSizeCategoryLarge]) {
return 0;
} else if ([sc isEqualToString:UIContentSizeCategoryExtraLarge]) {
return 1;
} else if ([sc isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
return 2;
} else if ([sc isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
return 3;
} else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityMedium]) {
return -11;
} else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityLarge]) {
return 10;
} else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge]) {
return 11;
} else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge]) {
return 12;
} else if ([sc isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]) {
return 13;
}
return -9999;
}
- (NSString *)getSysInfoByName:(char *)typeSpecifier
{
size_t size;
sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
char *answer = malloc(size);
sysctlbyname(typeSpecifier, answer, &size, NULL, 0);
NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
free(answer);
return results;
}
- (NSString *) getWifiNetworkName {
NSString *ssid = @"";
CFArrayRef interfaces = CNCopySupportedInterfaces();
if (interfaces) {
NSDictionary *info = (__bridge NSDictionary *)CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces, 0));
if (info) {
ssid = info[@"SSID"];
} else {
ssid = @"<<NONE>>";
}
} else {
ssid = @"<<NONE>>";
}
return ssid;
}
@end
|