-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathInstabugApi.m
400 lines (318 loc) · 16.2 KB
/
InstabugApi.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>
#import <Flutter/Flutter.h>
#import "Instabug.h"
#import "IBGNetworkLogger+CP.h"
#import "InstabugApi.h"
#import "ArgsRegistry.h"
#import "../Util/IBGAPM+PrivateAPIs.h"
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:((float)((rgbValue & 0xFF000000) >> 24)) / 255.0];
extern void InitInstabugApi(id<FlutterBinaryMessenger> messenger) {
InstabugApi *api = [[InstabugApi alloc] init];
InstabugHostApiSetup(messenger, api);
}
@implementation InstabugApi
- (void)setEnabledIsEnabled:(NSNumber *)isEnabled error:(FlutterError *_Nullable *_Nonnull)error {
Instabug.enabled = [isEnabled boolValue];
}
- (nullable NSNumber *)isBuiltWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
return @(YES);
}
- (nullable NSNumber *)isEnabledWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
return @(Instabug.enabled);
}
- (void)initToken:(NSString *)token invocationEvents:(NSArray<NSString *> *)invocationEvents debugLogsLevel:(NSString *)debugLogsLevel error:(FlutterError *_Nullable *_Nonnull)error {
SEL setPrivateApiSEL = NSSelectorFromString(@"setCurrentPlatform:");
if ([[Instabug class] respondsToSelector:setPrivateApiSEL]) {
NSInteger *platformID = IBGPlatformFlutter;
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[Instabug class] methodSignatureForSelector:setPrivateApiSEL]];
[inv setSelector:setPrivateApiSEL];
[inv setTarget:[Instabug class]];
[inv setArgument:&(platformID) atIndex:2];
[inv invoke];
}
// Disable automatic capturing of native iOS network logs to avoid duplicate
// logs of the same request when using a native network client like cupertino_http
[IBGNetworkLogger disableAutomaticCapturingOfNetworkLogs];
IBGInvocationEvent resolvedEvents = 0;
for (NSString *event in invocationEvents) {
resolvedEvents |= (ArgsRegistry.invocationEvents[event]).integerValue;
}
IBGSDKDebugLogsLevel resolvedLogLevel = (ArgsRegistry.sdkLogLevels[debugLogsLevel]).integerValue;
[Instabug setSdkDebugLogsLevel:resolvedLogLevel];
[Instabug startWithToken:token invocationEvents:resolvedEvents];
}
- (void)showWithError:(FlutterError *_Nullable *_Nonnull)error {
[Instabug show];
}
- (void)showWelcomeMessageWithModeMode:(NSString *)mode error:(FlutterError *_Nullable *_Nonnull)error {
IBGWelcomeMessageMode resolvedMode = (ArgsRegistry.welcomeMessageStates[mode]).integerValue;
[Instabug showWelcomeMessageWithMode:resolvedMode];
}
- (void)identifyUserEmail:(NSString *)email name:(nullable NSString *)name userId:(nullable NSString *)userId error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug identifyUserWithID:userId email:email name:name];
}
- (void)setUserDataData:(NSString *)data error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug setUserData:data];
}
- (void)logUserEventName:(NSString *)name error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug logUserEventWithName:name];
}
- (void)logOutWithError:(FlutterError *_Nullable *_Nonnull)error {
[Instabug logOut];
}
- (void)setLocaleLocale:(NSString *)locale error:(FlutterError *_Nullable *_Nonnull)error {
IBGLocale resolvedLocale = (ArgsRegistry.locales[locale]).integerValue;
[Instabug setLocale:resolvedLocale];
}
- (void)setColorThemeTheme:(NSString *)theme error:(FlutterError *_Nullable *_Nonnull)error {
IBGColorTheme resolvedTheme = (ArgsRegistry.colorThemes[theme]).integerValue;
[Instabug setColorTheme:resolvedTheme];
}
- (void)setWelcomeMessageModeMode:(NSString *)mode error:(FlutterError *_Nullable *_Nonnull)error {
IBGWelcomeMessageMode resolvedMode = (ArgsRegistry.welcomeMessageStates[mode]).integerValue;
[Instabug setWelcomeMessageMode:resolvedMode];
}
- (void)setPrimaryColorColor:(NSNumber *)color error:(FlutterError *_Nullable *_Nonnull)error {
Instabug.tintColor = UIColorFromRGB([color longValue]);
}
- (void)setSessionProfilerEnabledEnabled:(NSNumber *)enabled error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug setSessionProfilerEnabled:[enabled boolValue]];
}
- (void)setValueForStringWithKeyValue:(NSString *)value key:(NSString *)key error:(FlutterError *_Nullable *_Nonnull)error {
if ([ArgsRegistry.placeholders objectForKey:key]) {
NSString *resolvedKey = ArgsRegistry.placeholders[key];
[Instabug setValue:value forStringWithKey:resolvedKey];
}
else {
NSString *logMessage = [NSString stringWithFormat: @"%@%@%@", @"Instabug: ", key, @" is only relevant to Android."];
NSLog(@"%@", logMessage);
}
}
- (void)appendTagsTags:(NSArray<NSString *> *)tags error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug appendTags:tags];
}
- (void)resetTagsWithError:(FlutterError *_Nullable *_Nonnull)error {
[Instabug resetTags];
}
- (void)getTagsWithCompletion:(nonnull void (^)(NSArray<NSString *> * _Nullable, FlutterError * _Nullable))completion {
completion([Instabug getTags], nil);
}
- (void)addExperimentsExperiments:(NSArray<NSString *> *)experiments error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug addExperiments:experiments];
}
- (void)removeExperimentsExperiments:(NSArray<NSString *> *)experiments error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug removeExperiments:experiments];
}
- (void)clearAllExperimentsWithError:(FlutterError *_Nullable *_Nonnull)error {
[Instabug clearAllExperiments];
}
- (void)setUserAttributeValue:(NSString *)value key:(NSString *)key error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug setUserAttribute:value withKey:key];
}
- (void)removeUserAttributeKey:(NSString *)key error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug removeUserAttributeForKey:key];
}
- (void)getUserAttributeForKeyKey:(nonnull NSString *)key completion:(nonnull void (^)(NSString * _Nullable, FlutterError * _Nullable))completion {
completion([Instabug userAttributeForKey:key], nil);
}
- (void)getUserAttributesWithCompletion:(nonnull void (^)(NSDictionary<NSString *,NSString *> * _Nullable, FlutterError * _Nullable))completion {
completion(Instabug.userAttributes, nil);
}
- (void)setReproStepsConfigBugMode:(nullable NSString *)bugMode crashMode:(nullable NSString *)crashMode sessionReplayMode:(nullable NSString *)sessionReplayMode error:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
if (bugMode != nil) {
IBGUserStepsMode resolvedBugMode = ArgsRegistry.reproModes[bugMode].integerValue;
[Instabug setReproStepsFor:IBGIssueTypeBug withMode:resolvedBugMode];
}
if (crashMode != nil) {
IBGUserStepsMode resolvedCrashMode = ArgsRegistry.reproModes[crashMode].integerValue;
[Instabug setReproStepsFor:IBGIssueTypeCrash withMode:resolvedCrashMode];
}
if (sessionReplayMode != nil) {
IBGUserStepsMode resolvedSessionReplayMode = ArgsRegistry.reproModes[sessionReplayMode].integerValue;
[Instabug setReproStepsFor:IBGIssueTypeSessionReplay withMode:resolvedSessionReplayMode];
}
}
- (UIImage *)getImageForAsset:(NSString *)assetName {
NSString *key = [FlutterDartProject lookupKeyForAsset:assetName];
NSString *path = [[NSBundle mainBundle] pathForResource:key ofType:nil];
return [UIImage imageWithContentsOfFile:path];
}
- (void)setCustomBrandingImageLight:(NSString *)light dark:(NSString *)dark error:(FlutterError * _Nullable __autoreleasing *)error {
UIImage *lightImage = [self getImageForAsset:light];
UIImage *darkImage = [self getImageForAsset:dark];
if (!lightImage) {
lightImage = darkImage;
}
if (!darkImage) {
darkImage = lightImage;
}
if (@available(iOS 12.0, *)) {
UIImageAsset *imageAsset = [[UIImageAsset alloc] init];
[imageAsset registerImage:lightImage withTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]];
[imageAsset registerImage:darkImage withTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]];
Instabug.customBrandingImage = imageAsset;
} else {
UIImage *defaultImage = lightImage;
if (!lightImage) {
defaultImage = darkImage;
}
Instabug.customBrandingImage = defaultImage.imageAsset;
}
}
- (void)reportScreenChangeScreenName:(NSString *)screenName error:(FlutterError *_Nullable *_Nonnull)error {
SEL setPrivateApiSEL = NSSelectorFromString(@"logViewDidAppearEvent:");
if ([[Instabug class] respondsToSelector:setPrivateApiSEL]) {
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[Instabug class] methodSignatureForSelector:setPrivateApiSEL]];
[inv setSelector:setPrivateApiSEL];
[inv setTarget:[Instabug class]];
[inv setArgument:&(screenName) atIndex:2];
[inv invoke];
}
}
- (UIFont *)getFontForAsset:(NSString *)assetName error:(FlutterError *_Nullable *_Nonnull)error {
NSString *key = [FlutterDartProject lookupKeyForAsset:assetName];
NSString *path = [[NSBundle mainBundle] pathForResource:key ofType:nil];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
CFErrorRef fontError;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef) data);
CGFontRef cgFont = CGFontCreateWithDataProvider(provider);
UIFont *font;
if(!CTFontManagerRegisterGraphicsFont(cgFont, &fontError)){
CFStringRef errorDescription = CFErrorCopyDescription(fontError);
*error = [FlutterError errorWithCode:@"IBGFailedToLoadFont" message:(__bridge NSString *)errorDescription details:nil];
CFRelease(errorDescription);
} else {
NSString *fontName = (__bridge NSString *)CGFontCopyFullName(cgFont);
font = [UIFont fontWithName:fontName size:10.0];
}
if (cgFont) CFRelease(cgFont);
if (provider) CFRelease(provider);
return font;
}
- (void)setFontFont:(NSString *)fontAsset error:(FlutterError *_Nullable *_Nonnull)error {
UIFont *font = [self getFontForAsset:fontAsset error:error];
Instabug.font = font;
}
- (void)addFileAttachmentWithURLFilePath:(NSString *)filePath fileName:(NSString *)fileName error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug addFileAttachmentWithURL:[NSURL URLWithString:filePath]];
}
- (void)addFileAttachmentWithDataData:(FlutterStandardTypedData *)data fileName:(NSString *)fileName error:(FlutterError *_Nullable *_Nonnull)error {
[Instabug addFileAttachmentWithData:[data data]];
}
- (void)clearFileAttachmentsWithError:(FlutterError *_Nullable *_Nonnull)error {
[Instabug clearFileAttachments];
}
- (void)networkLogData:(NSDictionary<NSString *, id> *)data error:(FlutterError *_Nullable *_Nonnull)error {
NSString *url = data[@"url"];
NSString *method = data[@"method"];
NSString *requestBody = data[@"requestBody"];
NSString *responseBody = data[@"responseBody"];
int32_t responseCode = (int32_t) [data[@"responseCode"] integerValue];
int64_t requestBodySize = [data[@"requestBodySize"] integerValue];
int64_t responseBodySize = [data[@"responseBodySize"] integerValue];
int32_t errorCode = (int32_t) [data[@"errorCode"] integerValue];
NSString *errorDomain = data[@"errorDomain"];
NSDictionary *requestHeaders = data[@"requestHeaders"];
if ([requestHeaders count] == 0) {
requestHeaders = @{};
}
NSDictionary *responseHeaders = data[@"responseHeaders"];
NSString *contentType = data[@"responseContentType"];
int64_t duration = [data[@"duration"] integerValue];
int64_t startTime = [data[@"startTime"] integerValue] * 1000;
NSString *gqlQueryName = nil;
NSString *serverErrorMessage = nil;
NSNumber *isW3cHeaderFound = nil;
NSNumber *partialId = nil;
NSNumber *networkStartTimeInSeconds = nil;
NSString *w3CGeneratedHeader = nil;
NSString *w3CCaughtHeader = nil;
if (data[@"gqlQueryName"] != [NSNull null]) {
gqlQueryName = data[@"gqlQueryName"];
}
if (data[@"serverErrorMessage"] != [NSNull null]) {
serverErrorMessage = data[@"serverErrorMessage"];
}
if (data[@"partialId"] != [NSNull null]) {
partialId = data[@"partialId"];
}
if (data[@"isW3cHeaderFound"] != [NSNull null]) {
isW3cHeaderFound = data[@"isW3cHeaderFound"];
}
if (data[@"networkStartTimeInSeconds"] != [NSNull null]) {
networkStartTimeInSeconds = data[@"networkStartTimeInSeconds"];
}
if (data[@"w3CGeneratedHeader"] != [NSNull null]) {
w3CGeneratedHeader = data[@"w3CGeneratedHeader"];
}
if (data[@"w3CCaughtHeader"] != [NSNull null]) {
w3CCaughtHeader = data[@"w3CCaughtHeader"];
}
[IBGNetworkLogger addNetworkLogWithUrl:url
method:method
requestBody:requestBody
requestBodySize:requestBodySize
responseBody:responseBody
responseBodySize:responseBodySize
responseCode:responseCode
requestHeaders:requestHeaders
responseHeaders:responseHeaders
contentType:contentType
errorDomain:errorDomain
errorCode:errorCode
startTime:startTime
duration:duration
gqlQueryName:gqlQueryName
serverErrorMessage:serverErrorMessage
isW3cCaughted:isW3cHeaderFound
partialID:partialId
timestamp:networkStartTimeInSeconds
generatedW3CTraceparent:w3CGeneratedHeader
caughtedW3CTraceparent:w3CCaughtHeader];
}
- (void)willRedirectToStoreWithError:(FlutterError * _Nullable __autoreleasing *)error {
[Instabug willRedirectToAppStore];
}
- (void)setAutoMaskingEnabledIsEnabled:(NSNumber *)isEnabled error:(FlutterError *_Nullable *_Nonnull)error {
IBGNetworkLogger.autoMaskingEnabled = [isEnabled boolValue];
}
- (void)addFeatureFlagsFeatureFlagsMap:(nonnull NSDictionary<NSString *,NSString *> *)featureFlagsMap error:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
NSMutableArray<IBGFeatureFlag *> *featureFlags = [NSMutableArray array];
for(id key in featureFlagsMap){
NSString* variant =((NSString * )[featureFlagsMap objectForKey:key]);
if ([variant length]==0) {
[featureFlags addObject:[[IBGFeatureFlag alloc] initWithName:key]];
}
else{
[featureFlags addObject:[[IBGFeatureFlag alloc] initWithName:key variant:variant]];
}
}
[Instabug addFeatureFlags:featureFlags];
}
- (void)removeAllFeatureFlagsWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
[Instabug removeAllFeatureFlags];
}
- (void)removeFeatureFlagsFeatureFlags:(nonnull NSArray<NSString *> *)featureFlags error:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
NSMutableArray<IBGFeatureFlag *> *features = [NSMutableArray array];
for(id item in featureFlags){
[features addObject:[[IBGFeatureFlag alloc] initWithName:item]];
}
@try {
[Instabug removeFeatureFlags:features];
} @catch (NSException *exception) {
NSLog(@"%@", exception);
}
}
- (void)registerFeatureFlagChangeListenerWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
// Android only. We still need this method to exist to match the Pigeon-generated protocol.
}
- (nullable NSDictionary<NSString *,NSNumber *> *)isW3CFeatureFlagsEnabledWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
NSDictionary<NSString * , NSNumber *> *result= @{
@"isW3cExternalTraceIDEnabled":[NSNumber numberWithBool:IBGNetworkLogger.w3ExternalTraceIDEnabled] ,
@"isW3cExternalGeneratedHeaderEnabled":[NSNumber numberWithBool:IBGNetworkLogger.w3ExternalGeneratedHeaderEnabled] ,
@"isW3cCaughtHeaderEnabled":[NSNumber numberWithBool:IBGNetworkLogger.w3CaughtHeaderEnabled] ,
};
return result;
}
@end