Skip to content

Commit 429d609

Browse files
committed
Update iOS SDK usage and add try/catch blocks
1 parent acab4d3 commit 429d609

1 file changed

Lines changed: 70 additions & 38 deletions

File tree

Assets/Plugins/iOS/mParticleUnity.m

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ + (MPIdentityApiRequest *)MPIdentityApiRequest:(NSDictionary *)json;
2525

2626
NSString *jsonString = [[NSString alloc] initWithCString:json encoding:NSUTF8StringEncoding];
2727
NSError *error = nil;
28-
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
29-
options:0
30-
error:&error];
28+
NSDictionary *dictionary = nil;
29+
@try {
30+
dictionary = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
31+
options:0
32+
error:&error];
33+
} @catch (NSException *exception) {
34+
NSLog(@"%@", exception);
35+
}
3136

3237
if (error) {
3338
dictionary = nil;
@@ -37,9 +42,14 @@ + (MPIdentityApiRequest *)MPIdentityApiRequest:(NSDictionary *)json;
3742

3843
NSDictionary *dictionaryWithJSONString(NSString *jsonString) {
3944
NSError *error = nil;
40-
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
41-
options:0
42-
error:&error];
45+
NSDictionary *dictionary = nil;
46+
@try {
47+
dictionary = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
48+
options:0
49+
error:&error];
50+
} @catch (NSException *exception) {
51+
NSLog(@"%@", exception);
52+
}
4353

4454
if (error) {
4555
dictionary = nil;
@@ -49,9 +59,15 @@ + (MPIdentityApiRequest *)MPIdentityApiRequest:(NSDictionary *)json;
4959

5060
NSString *jsonWithDictionary(NSDictionary *dictionary) {
5161
NSError *error;
52-
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
53-
options:0
54-
error:&error];
62+
NSData *jsonData = nil;
63+
@try {
64+
jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
65+
options:0
66+
error:&error];
67+
} @catch (NSException *exception) {
68+
NSLog(@"%@", exception);
69+
}
70+
5571
if (!jsonData) {
5672
NSLog(@"Error while writing NSDictionary to JSON: %@", error);
5773
}
@@ -83,7 +99,7 @@ + (MPIdentityApiRequest *)MPIdentityApiRequest:(NSDictionary *)json;
8399
}
84100

85101
char *toChar(NSString *value) {
86-
char *string = [value UTF8String];
102+
char *string = (char *)[value UTF8String];
87103
if (string == nil)
88104
return NULL;
89105

@@ -111,27 +127,27 @@ void _Initialize (const char *optionsJSON) {
111127
static dispatch_once_t onceToken;
112128
NSDictionary *optionsDictionary = dictionaryWithJSON(optionsJSON);
113129
MParticleOptions *options = [MPUnityConvert MParticleOptions:optionsDictionary];
114-
dispatch_once(&onceToken, ^{
115-
[[MParticle sharedInstance] startWithOptions:options];
116-
});
117130
if ([[optionsDictionary allKeys]containsObject:@"UploadInterval"]) {
118131
int value = [optionsDictionary[@"UploadInterval"]intValue];
119132
if (value >= 0) {
120-
[MParticle sharedInstance].uploadInterval = value;
133+
options.uploadInterval = value;
121134
}
122135
}
123136
if ([[optionsDictionary allKeys]containsObject:@"SessionTimeout"]) {
124137
int value = [optionsDictionary[@"SessionTimeout"]intValue];
125138
if (value >= 0) {
126-
[MParticle sharedInstance].sessionTimeout = value;
139+
options.sessionTimeout = value;
127140
}
128141
}
129142
if ([[optionsDictionary allKeys]containsObject:@"LogLevel"]) {
130143
int logLevel = [optionsDictionary[@"LogLevel"]intValue];
131144
if (logLevel >= 0) {
132-
[MParticle sharedInstance].logLevel = (MPILogLevel)logLevel;
145+
options.logLevel = (MPILogLevel)logLevel;
133146
}
134147
}
148+
dispatch_once(&onceToken, ^{
149+
[[MParticle sharedInstance] startWithOptions:options];
150+
});
135151
}
136152

137153
void _SetOptOut(int optOut) {
@@ -145,7 +161,7 @@ void _LogEvent(const char *mpEventJSON) {
145161
if ([[json allKeys]containsObject:@"Info"]) {
146162
NSDictionary *eventInfo = json[@"Info"];
147163
if (eventInfo.count > 0) {
148-
event.info = eventInfo;
164+
event.customAttributes = eventInfo;
149165
}
150166
}
151167
NSNumber *duration = json[@"Duration"];
@@ -174,7 +190,7 @@ void _LogCommerceEvent(const char *commerceEventJSON) {
174190
NSDictionary *json = dictionaryWithJSON(commerceEventJSON);
175191

176192
MPCommerceEvent *commerceEvent = [MPUnityConvert MPCommerceEvent:json];
177-
[[MParticle sharedInstance] logCommerceEvent:commerceEvent];
193+
[[MParticle sharedInstance] logEvent:commerceEvent];
178194
}
179195

180196
void _LogScreen(const char *screenName) {
@@ -188,15 +204,14 @@ void _LeaveBreadcrumb(const char *breadcrumbName) {
188204
}
189205

190206
int _GetEnvironment() {
191-
int environment = [MParticle sharedInstance].environment;
207+
int environment = (int)[MParticle sharedInstance].environment;
192208
return environment;
193209
}
194210

195211
void _SetUploadInterval(int uploadInterval) {
196-
[[MParticle sharedInstance] setUploadInterval:uploadInterval];
212+
NSLog(@"Warning: Upload interval could not be set, please specify the interval in SDK initialization options");
197213
}
198214

199-
200215
char* _Identity_Identify(const char *identityApiRequestJSON) {
201216
NSDictionary *identityRequestDict = dictionaryWithJSON(identityApiRequestJSON);
202217
MPIdentityApiRequest *identityApiRequest = [MPUnityConvert MPIdentityApiRequest:identityRequestDict];
@@ -341,9 +356,15 @@ void _Identity_AddIdentityStateListener(){
341356
usingBlock: ^ (NSNotification * note) {
342357
NSDictionary *values = @{@"Mpid": [((MParticleUser *) note.userInfo[mParticleUserKey]).userId stringValue]};
343358
NSError *error;
344-
NSData *jsonData =[NSJSONSerialization dataWithJSONObject:values
345-
options:0
346-
error:&error];
359+
NSData *jsonData = nil;
360+
@try {
361+
jsonData =[NSJSONSerialization dataWithJSONObject:values
362+
options:0
363+
error:&error];
364+
} @catch (NSException *exception) {
365+
NSLog(@"%@", exception);
366+
}
367+
347368
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
348369
char *message = toChar(jsonString);
349370
UnitySendMessage("MParticle", "OnUserIdentified", message);
@@ -366,7 +387,7 @@ void _Identity_RemoveIdentityStateListener(){
366387
return toChar([currentUser.userId stringValue]);
367388
}
368389
@catch (NSException *ex) {
369-
NSLog(ex);
390+
NSLog(@"%@", ex);
370391
return toChar(@"0");
371392
}
372393
}
@@ -383,7 +404,7 @@ void _Upload() {
383404
return toChar(@"true");
384405
}
385406
@catch (NSException *ex) {
386-
NSLog(ex);
407+
NSLog(@"%@", ex);
387408
return toChar(@"false");
388409
}
389410
}
@@ -396,7 +417,7 @@ void _Upload() {
396417
return toChar(@"true");
397418
}
398419
@catch(NSException *ex) {
399-
NSLog(ex);
420+
NSLog(@"%@", ex);
400421
return toChar(@"false");
401422
}
402423
}
@@ -408,7 +429,7 @@ void _Upload() {
408429
return toChar(@"true");
409430
}
410431
@catch (NSException *ex) {
411-
NSLog(ex);
432+
NSLog(@"%@", ex);
412433
return toChar(@"false");
413434
}
414435
}
@@ -420,7 +441,7 @@ void _Upload() {
420441
return toChar(@"true");
421442
}
422443
@catch (NSException *ex) {
423-
NSLog(ex);
444+
NSLog(@"%@", ex);
424445
return toChar(@"false");
425446
}
426447
}
@@ -430,14 +451,20 @@ void _Upload() {
430451
NSString *mpidString = [[NSString alloc] initWithCString:mpid encoding:NSUTF8StringEncoding];
431452
NSDictionary *userAttributes = [[[MParticle sharedInstance].identity getUser:(@([mpidString longLongValue]))] userAttributes];
432453
NSError *error;
433-
NSData *jsonData =[NSJSONSerialization dataWithJSONObject:userAttributes
434-
options:0
435-
error:&error];
454+
NSData *jsonData = nil;
455+
@try {
456+
jsonData =[NSJSONSerialization dataWithJSONObject:userAttributes
457+
options:0
458+
error:&error];
459+
} @catch (NSException *exception) {
460+
NSLog(@"%@", exception);
461+
}
462+
436463
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
437464
return toChar(jsonString);
438465
}
439466
@catch (NSException *ex) {
440-
NSLog(ex);
467+
NSLog(@"%@", ex);
441468
return toChar(@"{}");
442469
}
443470
}
@@ -451,14 +478,19 @@ void _Upload() {
451478
for (id key in userIdentities) {
452479
[userIdentityStrings setValue:userIdentities[key] forKey:[key stringValue]];
453480
}
454-
NSData *jsonData =[NSJSONSerialization dataWithJSONObject:userIdentityStrings
455-
options:0
456-
error:&error];
481+
NSData *jsonData = nil;
482+
@try {
483+
jsonData =[NSJSONSerialization dataWithJSONObject:userIdentityStrings
484+
options:0
485+
error:&error];
486+
} @catch (NSException *exception) {
487+
NSLog(@"%@", exception);
488+
}
457489
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
458490
return toChar(jsonString);
459491
}
460492
@catch (NSException *ex) {
461-
NSLog(ex);
493+
NSLog(@"%@", ex);
462494
return toChar(@"{}");
463495
}
464496
}
@@ -469,7 +501,7 @@ void _Upload() {
469501
return toChar([currentMpid stringValue]);
470502
}
471503
@catch (NSException *ex) {
472-
NSLog(ex);
504+
NSLog(@"%@", ex);
473505
return toChar(@"false");
474506
}
475507
}

0 commit comments

Comments
 (0)