-
Notifications
You must be signed in to change notification settings - Fork 121
/
BTAnalyticsMetadata.m
executable file
·242 lines (199 loc) · 7.97 KB
/
BTAnalyticsMetadata.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
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
#import "BTAnalyticsMetaData.h"
#import "Braintree-Version.h"
#import "BTKeychain.h"
@import CoreLocation;
#import <sys/sysctl.h>
#import <sys/utsname.h>
#import <UIKit/UIKit.h>
#ifdef __IPHONE_8_0
#define kBTCLAuthorizationStatusAuthorized kCLAuthorizationStatusAuthorizedAlways
#else
#define kBTCLAuthorizationStatusAuthorized kCLAuthorizationStatusAuthorized
#endif
@implementation BTAnalyticsMetadata
+ (NSDictionary *)metadata {
BTAnalyticsMetadata *m = [[BTAnalyticsMetadata alloc] init];
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:16];
[self setObject:[m platform] forKey:@"platform" inDictionary:data];
[self setObject:[m platformVersion] forKey:@"platformVersion" inDictionary:data];
[self setObject:[m sdkVersion] forKey:@"sdkVersion" inDictionary:data];
[self setObject:[m merchantAppId] forKey:@"merchantAppId" inDictionary:data];
[self setObject:[m merchantAppName] forKey:@"merchantAppName" inDictionary:data];
[self setObject:[m merchantAppVersion] forKey:@"merchantAppVersion" inDictionary:data];
#ifndef __IPHONE_8_0
[self setObject:@([m deviceRooted]) forKey:@"deviceRooted" inDictionary:data];
#endif
[self setObject:[m deviceManufacturer] forKey:@"deviceManufacturer" inDictionary:data];
[self setObject:[m deviceModel] forKey:@"deviceModel" inDictionary:data];
if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kBTCLAuthorizationStatusAuthorized) {
[self setObject:@([m deviceLocationLatitude]) forKey:@"deviceLocationLatitude" inDictionary:data];
[self setObject:@([m deviceLocationLongitude]) forKey:@"deviceLocationLongitude" inDictionary:data];
}
[self setObject:[m iosDeviceName] forKey:@"iosDeviceName" inDictionary:data];
[self setObject:[m iosSystemName] forKey:@"iosSystemName" inDictionary:data];
[self setObject:[m iosBaseSDK] forKey:@"iosBaseSDK" inDictionary:data];
[self setObject:[m iosDeploymentTarget] forKey:@"iosDeploymentTarget" inDictionary:data];
[self setObject:[m iosIdentifierForVendor] forKey:@"iosIdentifierForVendor" inDictionary:data];
[self setObject:@([m iosIsCocoapods]) forKey:@"iosIsCocoapods" inDictionary:data];
[self setObject:[m deviceAppGeneratedPersistentUuid] forKey:@"deviceAppGeneratedPersistentUuid" inDictionary:data];
[self setObject:@([m isSimulator]) forKey:@"isSimulator" inDictionary:data];
[self setObject:[m deviceScreenOrientation] forKey:@"deviceScreenOrientation" inDictionary:data];
[self setObject:[m userInterfaceOrientation] forKey:@"userInterfaceOrientation" inDictionary:data];
[self setObject:@([m isPaypalInstalled]) forKey:@"paypalInstalled" inDictionary:data];
[self setObject:@([m isVenmoInstalled]) forKey:@"venmoInstalled" inDictionary:data];
return [NSDictionary dictionaryWithDictionary:data];
}
+ (void)setObject:(id)object forKey:(id<NSCopying>)aKey inDictionary:(NSMutableDictionary *)dictionary {
if (object) {
[dictionary setObject:object forKey:aKey];
}
}
#pragma mark Metadata Factors
- (NSString *)platform {
return @"iOS";
}
- (NSString *)platformVersion {
return [[UIDevice currentDevice] systemVersion];
}
- (NSString *)sdkVersion {
return BRAINTREE_VERSION;
}
- (NSString *)merchantAppId {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey];
}
- (NSString *)merchantAppVersion {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey];
}
- (NSString *)merchantAppName {
return [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleNameKey];
}
- (BOOL)deviceRooted {
#if TARGET_IPHONE_SIMULATOR || __IPHONE_8_0
return NO;
#else
BOOL isJailbroken = system(NULL) == 1;
return isJailbroken;
#endif
}
- (NSString *)deviceManufacturer {
return @"Apple";
}
- (NSString *)deviceModel {
struct utsname systemInfo;
uname(&systemInfo);
NSString* code = [NSString stringWithCString:systemInfo.machine
encoding:NSUTF8StringEncoding];
return code;
}
- (CLLocationDegrees)deviceLocationLatitude {
return [[[[CLLocationManager alloc] init] location] coordinate].latitude;
}
- (CLLocationDegrees)deviceLocationLongitude {
return [[[[CLLocationManager alloc] init] location] coordinate].longitude;
}
- (NSString *)iosIdentifierForVendor {
return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
- (NSString *)iosDeploymentTarget {
return [@(__IPHONE_OS_VERSION_MIN_REQUIRED) stringValue];
}
- (NSString *)iosBaseSDK {
return [@(__IPHONE_OS_VERSION_MAX_ALLOWED) stringValue];
}
- (NSString *)iosDeviceName {
return [[UIDevice currentDevice] name];
}
- (NSString *)iosSystemName {
return [[UIDevice currentDevice] systemName];
}
- (BOOL)iosIsCocoapods {
#ifdef COCOAPODS
return YES;
#else
return NO;
#endif
}
- (NSString *)deviceAppGeneratedPersistentUuid {
@try {
static NSString *deviceAppGeneratedPersistentUuidKeychainKey = @"deviceAppGeneratedPersistentUuid";
NSString *savedIdentifier = [BTKeychain stringForKey:deviceAppGeneratedPersistentUuidKeychainKey];
if (savedIdentifier.length == 0) {
savedIdentifier = [[NSUUID UUID] UUIDString];
BOOL setDidSucceed = [BTKeychain setString:savedIdentifier
forKey:deviceAppGeneratedPersistentUuidKeychainKey];
if (!setDidSucceed) {
return nil;
}
}
return savedIdentifier;
} @catch (NSException *exception) {
return nil;
}
}
- (BOOL)isSimulator {
return TARGET_IPHONE_SIMULATOR;
}
- (NSString *)userInterfaceOrientation {
// UIViewController interface orientation methods are deprecated as of iOS 8
#ifndef __IPHONE_8_0
if ([UIApplication class] == nil) {
return nil;
}
UIInterfaceOrientation deviceOrientation = [[[[UIApplication sharedApplication] keyWindow] rootViewController] interfaceOrientation];
switch (deviceOrientation) {
case UIInterfaceOrientationPortrait:
return @"Portrait";
case UIInterfaceOrientationPortraitUpsideDown:
return @"PortraitUpsideDown";
case UIInterfaceOrientationLandscapeLeft:
return @"LandscapeLeft";
case UIInterfaceOrientationLandscapeRight:
return @"LandscapeRight";
default:
return @"Unknown";
}
#else
return nil;
#endif
}
- (NSString *)deviceScreenOrientation {
if ([UIDevice class] == nil) {
return nil;
}
switch ([[UIDevice currentDevice] orientation]) {
case UIDeviceOrientationFaceUp:
return @"FaceUp";
case UIDeviceOrientationFaceDown:
return @"FaceDown";
case UIDeviceOrientationPortrait:
return @"Portrait";
case UIDeviceOrientationPortraitUpsideDown:
return @"PortraitUpsideDown";
case UIDeviceOrientationLandscapeLeft:
return @"LandscapeLeft";
case UIDeviceOrientationLandscapeRight:
return @"LandscapeRight";
default:
return @"Unknown";
}
}
- (BOOL)isPaypalInstalled {
static BOOL paypalInstalled;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURL *paypalV1URL = [NSURL URLWithString:@"com.paypal.ppclient.touch.v1://"];
NSURL *paypalV2URL = [NSURL URLWithString:@"com.paypal.ppclient.touch.v2://"];
paypalInstalled = [[UIApplication sharedApplication] canOpenURL:paypalV1URL] || [[UIApplication sharedApplication] canOpenURL:paypalV2URL];
});
return paypalInstalled;
}
- (BOOL)isVenmoInstalled {
static BOOL venmoInstalled;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURL *venmoURL = [NSURL URLWithString:@"com.venmo.touch.v2://x-callback-url/vzero/auth"];
venmoInstalled = [[UIApplication sharedApplication] canOpenURL:venmoURL];
});
return venmoInstalled;
}
@end