Skip to content
This repository was archived by the owner on Jul 22, 2020. It is now read-only.

Commit 5998f7c

Browse files
committed
Completion blocks for various calls (#17)
* add onSuccess and onFailure block params to functions that call Iterable API * add additional overload for trackPushOpen * update changelog
1 parent 6844075 commit 5998f7c

File tree

3 files changed

+137
-13
lines changed

3 files changed

+137
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
66
#### Added
7-
- nothing yet
7+
- completion handler blocks for all the Iterable APIs
88

99
#### Removed
1010
- nothing yet

Iterable-iOS-SDK/IterableAPI.h

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
133133
*/
134134
- (void)registerToken:(NSData *)token appName:(NSString *)appName pushServicePlatform:(PushServicePlatform)pushServicePlatform;
135135

136+
/*!
137+
@method
138+
139+
@abstract Register this device's token with Iterable with custom completion blocks
140+
141+
@param token The token representing this device/application pair, obtained from
142+
`application:didRegisterForRemoteNotificationsWithDeviceToken`
143+
after registering for remote notifications
144+
@param appName The application name, as configured in Iterable during set up of the push integration
145+
@param pushServicePlatform The PushServicePlatform to use for this device; dictates whether to register this token in the sandbox or production environment
146+
@param onSuccess OnSuccessHandler to invoke if token registration is successful
147+
@param onFailure OnFailureHandler to invoke if token registration fails
148+
149+
@see PushServicePlatform
150+
@see OnSuccessHandler
151+
@see OnFailureHandler
152+
*/
153+
- (void)registerToken:(NSData *)token appName:(NSString *)appName pushServicePlatform:(PushServicePlatform)pushServicePlatform onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure;
154+
136155
/////////////////////////
137156
/// @name Tracking events
138157
/////////////////////////
@@ -154,7 +173,7 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
154173
/*!
155174
@method
156175
157-
@abstract Tracks a purchase
176+
@abstract Tracks a purchase with additional data
158177
159178
@discussion Pass in the total purchase amount and an `NSArray` of `CommerceItem`s
160179
@@ -169,7 +188,26 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
169188
/*!
170189
@method
171190
172-
@abstract Tracks a pushOpen event.
191+
@abstract Tracks a purchase with additional data and custom completion blocks
192+
193+
@discussion Pass in the total purchase amount and an `NSArray` of `CommerceItem`s
194+
195+
@param total total purchase amount
196+
@param items list of purchased items
197+
@param dataFields an `NSDictionary` containing any additional information to save along with the event
198+
@param onSuccess OnSuccessHandler to invoke if the purchase is tracked successfully
199+
@param onFailure OnFailureHandler to invoke if tracking the purchase fails
200+
201+
@see CommerceItem
202+
@see OnSuccessHandler
203+
@see OnFailureHandler
204+
*/
205+
- (void)trackPurchase:(NSNumber *)total items:(NSArray<CommerceItem>*)items dataFields:(nullable NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure;
206+
207+
/*!
208+
@method
209+
210+
@abstract Tracks a pushOpen event with a push notification payload
173211
174212
@discussion Pass in the `userInfo` from the push notification payload
175213
@@ -180,7 +218,7 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
180218
/*!
181219
@method
182220
183-
@abstract Tracks a pushOpen event.
221+
@abstract Tracks a pushOpen event with a push notification and optional additional data
184222
185223
@discussion Pass in the `userInfo` from the push notification payload
186224
@@ -192,7 +230,24 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
192230
/*!
193231
@method
194232
195-
@abstract Tracks a pushOpen event
233+
@abstract Tracks a pushOpen event with a push notification, optional additional data, and custom completion blocks
234+
235+
@discussion Pass in the `userInfo` from the push notification payload
236+
237+
@param userInfo the push notification payload
238+
@param dataFields an `NSDictionary` containing any additional information to save along with the event
239+
@param onSuccess OnSuccessHandler to invoke if the open is tracked successfully
240+
@param onFailure OnFailureHandler to invoke if tracking the open fails
241+
242+
@see OnSuccessHandler
243+
@see OnFailureHandler
244+
*/
245+
- (void)trackPushOpen:(NSDictionary *)userInfo dataFields:(nullable NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure;
246+
247+
/*!
248+
@method
249+
250+
@abstract Tracks a pushOpen event for the specified campaign and template ids, whether the app was already running when the push was received, and optional additional data
196251
197252
@discussion Pass in the the relevant campaign data
198253
@@ -201,7 +256,26 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
201256
@param appAlreadyRunning This will get merged into the dataFields. Whether the app is already running when the notification was received
202257
@param dataFields An `NSDictionary` containing any additional information to save along with the event
203258
*/
204-
- (void)trackPushOpen:(NSNumber *)campaignId templateId:(NSNumber *)templateId appAlreadyRunning:(BOOL)appAlreadyRunning dataFields:(NSDictionary *)dataFields;
259+
- (void)trackPushOpen:(NSNumber *)campaignId templateId:(NSNumber *)templateId appAlreadyRunning:(BOOL)appAlreadyRunning dataFields:(nullable NSDictionary *)dataFields;
260+
261+
/*!
262+
@method
263+
264+
@abstract Tracks a pushOpen event for the specified campaign and template ids, whether the app was already running when the push was received, and optional additional data, with custom completion blocks
265+
266+
@discussion Pass in the the relevant campaign data
267+
268+
@param campaignId The campaignId of the the push notification that caused this open event
269+
@param templateId The templateId of the the push notification that caused this open event
270+
@param appAlreadyRunning This will get merged into the dataFields. Whether the app is already running when the notification was received
271+
@param dataFields An `NSDictionary` containing any additional information to save along with the event
272+
@param onSuccess OnSuccessHandler to invoke if the open is tracked successfully
273+
@param onFailure OnFailureHandler to invoke if tracking the open fails
274+
275+
@see OnSuccessHandler
276+
@see OnFailureHandler
277+
*/
278+
- (void)trackPushOpen:(NSNumber *)campaignId templateId:(NSNumber *)templateId appAlreadyRunning:(BOOL)appAlreadyRunning dataFields:(nullable NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure;
205279

206280
/*!
207281
@method
@@ -217,7 +291,7 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
217291
/*!
218292
@method
219293
220-
@abstract Tracks a custom event.
294+
@abstract Tracks a custom event with optional additional fields
221295
222296
@discussion Pass in the the custom event data.
223297
@@ -226,6 +300,23 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
226300
*/
227301
- (void)track:(NSString *)eventName dataFields:(nullable NSDictionary *)dataFields;
228302

303+
/*!
304+
@method
305+
306+
@abstract Tracks a custom event with optional additional fields and custom completion blocks
307+
308+
@discussion Pass in the the custom event data.
309+
310+
@param eventName Name of the event
311+
@param dataFields An `NSDictionary` containing any additional information to save along with the event
312+
@param onSuccess OnSuccessHandler to invoke if the track call succeeds
313+
@param onFailure OnFailureHandler to invoke if the track call fails
314+
315+
@see OnSuccessHandler
316+
@see OnFailureHandler
317+
*/
318+
- (void)track:(NSString *)eventName dataFields:(nullable NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure;
319+
229320
@end
230321

231322
NS_ASSUME_NONNULL_END

Iterable-iOS-SDK/IterableAPI.m

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ + (IterableAPI *)sharedInstanceWithApiKey:(NSString *)apiKey andEmail:(NSString
301301

302302
// documented in IterableAPI.h
303303
- (void)registerToken:(NSData *)token appName:(NSString *)appName pushServicePlatform:(PushServicePlatform)pushServicePlatform
304+
{
305+
[self registerToken:token appName:appName pushServicePlatform:pushServicePlatform onSuccess:[IterableAPI defaultOnSuccess:@"registerToken"] onFailure:[IterableAPI defaultOnFailure:@"registerToken"]];
306+
}
307+
308+
// documented in IterableAPI.h
309+
- (void)registerToken:(NSData *)token appName:(NSString *)appName pushServicePlatform:(PushServicePlatform)pushServicePlatform onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure
304310
{
305311
NSString *hexToken = [token hexadecimalString];
306312

@@ -334,7 +340,7 @@ - (void)registerToken:(NSData *)token appName:(NSString *)appName pushServicePla
334340
};
335341
LogDebug(@"sending registerToken request with args %@", args);
336342
NSURLRequest *request = [self createRequestForAction:@"users/registerDeviceToken" withArgs:args];
337-
[self sendRequest:request onSuccess:[IterableAPI defaultOnSuccess:@"registerToken"] onFailure:[IterableAPI defaultOnFailure:@"registerToken"]];
343+
[self sendRequest:request onSuccess:onSuccess onFailure:onFailure];
338344
}
339345
}
340346

@@ -346,6 +352,12 @@ - (void)track:(NSString *)eventName
346352

347353
// documented in IterableAPI.h
348354
- (void)track:(NSString *)eventName dataFields:(NSDictionary *)dataFields
355+
{
356+
[self track:eventName dataFields:dataFields onSuccess:[IterableAPI defaultOnSuccess:@"track"] onFailure:[IterableAPI defaultOnFailure:@"track"]];
357+
}
358+
359+
// documented in IterableAPI.h
360+
- (void)track:(NSString *)eventName dataFields:(NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure
349361
{
350362
NSDictionary *args;
351363
if (dataFields) {
@@ -363,7 +375,7 @@ - (void)track:(NSString *)eventName dataFields:(NSDictionary *)dataFields
363375

364376
}
365377
NSURLRequest *request = [self createRequestForAction:@"events/track" withArgs:args];
366-
[self sendRequest:request onSuccess:[IterableAPI defaultOnSuccess:@"track"] onFailure:[IterableAPI defaultOnFailure:@"track"]];
378+
[self sendRequest:request onSuccess:onSuccess onFailure:onFailure];
367379
}
368380

369381
// documented in IterableAPI.h
@@ -374,21 +386,35 @@ - (void)trackPushOpen:(NSDictionary *)userInfo
374386

375387
// documented in IterableAPI.h
376388
- (void)trackPushOpen:(NSDictionary *)userInfo dataFields:(NSDictionary *)dataFields
389+
{
390+
[self trackPushOpen:userInfo dataFields:dataFields onSuccess:[IterableAPI defaultOnSuccess:@"trackPushOpen"] onFailure:[IterableAPI defaultOnFailure:@"trackPushOpen"]];
391+
}
392+
393+
// documented in IterableAPI.h
394+
- (void)trackPushOpen:(NSDictionary *)userInfo dataFields:(NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure
377395
{
378396
LogDebug(@"tracking push open");
379397

380398
if (userInfo && userInfo[@"itbl"]) {
381399
NSDictionary *pushData = userInfo[@"itbl"];
382400
if ([pushData isKindOfClass:[NSDictionary class]] && pushData[@"campaignId"] && pushData[@"templateId"]) {
383-
[self trackPushOpen:pushData[@"campaignId"] templateId:pushData[@"templateId"] appAlreadyRunning:false dataFields:dataFields];
401+
[self trackPushOpen:pushData[@"campaignId"] templateId:pushData[@"templateId"] appAlreadyRunning:false dataFields:dataFields onSuccess:onSuccess onFailure:onFailure];
384402
} else {
385-
LogError(@"error tracking push open");
403+
if (onFailure) {
404+
onFailure(@"Not tracking push open - payload is not an Iterable notification", [[NSData alloc] init]);
405+
}
386406
}
387407
}
388408
}
389409

390410
// documented in IterableAPI.h
391411
- (void)trackPushOpen:(NSNumber *)campaignId templateId:(NSNumber *)templateId appAlreadyRunning:(BOOL)appAlreadyRunning dataFields:(NSDictionary *)dataFields
412+
{
413+
[self trackPushOpen:campaignId templateId:templateId appAlreadyRunning:appAlreadyRunning dataFields:dataFields onSuccess:[IterableAPI defaultOnSuccess:@"trackPushOpen"] onFailure:[IterableAPI defaultOnFailure:@"trackPushOpen"]];
414+
}
415+
416+
// documented in IterableAPI.h
417+
- (void)trackPushOpen:(NSNumber *)campaignId templateId:(NSNumber *)templateId appAlreadyRunning:(BOOL)appAlreadyRunning dataFields:(NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure
392418
{
393419
NSMutableDictionary *reqDataFields;
394420
if (dataFields) {
@@ -405,7 +431,8 @@ - (void)trackPushOpen:(NSNumber *)campaignId templateId:(NSNumber *)templateId a
405431
@"dataFields": reqDataFields
406432
};
407433
NSURLRequest *request = [self createRequestForAction:@"events/trackPushOpen" withArgs:args];
408-
[self sendRequest:request onSuccess:[IterableAPI defaultOnSuccess:@"trackPushOpen"] onFailure:[IterableAPI defaultOnFailure:@"trackPushOpen"]];}
434+
[self sendRequest:request onSuccess:onSuccess onFailure:onFailure];
435+
}
409436

410437
// documented in IterableAPI.h
411438
- (void)trackPurchase:(NSNumber *)total items:(NSArray<CommerceItem> *)items
@@ -415,6 +442,12 @@ - (void)trackPurchase:(NSNumber *)total items:(NSArray<CommerceItem> *)items
415442

416443
// documented in IterableAPI.h
417444
- (void)trackPurchase:(NSNumber *)total items:(NSArray<CommerceItem> *)items dataFields:(NSDictionary *)dataFields
445+
{
446+
[self trackPurchase:total items:items dataFields:dataFields onSuccess:[IterableAPI defaultOnSuccess:@"trackPurchase"] onFailure:[IterableAPI defaultOnFailure:@"trackPurchase"]];
447+
}
448+
449+
// documented in IterableAPI.h
450+
- (void)trackPurchase:(NSNumber *)total items:(NSArray<CommerceItem> *)items dataFields:(NSDictionary *)dataFields onSuccess:(OnSuccessHandler)onSuccess onFailure:(OnFailureHandler)onFailure
418451
{
419452
NSDictionary *args;
420453

@@ -442,6 +475,6 @@ - (void)trackPurchase:(NSNumber *)total items:(NSArray<CommerceItem> *)items dat
442475
};
443476
}
444477
NSURLRequest *request = [self createRequestForAction:@"commerce/trackPurchase" withArgs:args];
445-
[self sendRequest:request onSuccess:[IterableAPI defaultOnSuccess:@"trackPurchase"] onFailure:[IterableAPI defaultOnFailure:@"trackPurchase"]];}
478+
[self sendRequest:request onSuccess:onSuccess onFailure:onFailure];}
446479

447480
@end

0 commit comments

Comments
 (0)