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

Commit c4aff00

Browse files
authored
Feature/itbl 3347 universal link redirects (#44)
* Working version of the no-redirect call to dataTaskWithURL. Need to verify the effects on our existing api calls if those hit a re-directed link. Perhaps we should create a different session for the getAndTrack call which specifies the re-direct logic. * Removes the extra reference to the session. * Updated version which abstracts out the NSURLSessionDelegate from IterableApi for deep linking * fixed incorrect static function reference * updated names for the urlSession to be redirectUrlSession not to be confused with the iterableApi session * fix name redirectUrlSession * Refactor of IterableDeeplink into IterableDeeplinkManager Changed getAndTrackDeeplink back to be static since it doesn't require an instance of IterableApi * updating documentation and minor changes to static fields
1 parent 9e1656a commit c4aff00

File tree

4 files changed

+142
-18
lines changed

4 files changed

+142
-18
lines changed

Iterable-iOS-SDK/IterableAPI.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,9 @@ typedef NS_ENUM(NSInteger, PushServicePlatform) {
572572
@param webpageURL the URL that was clicked
573573
@param callbackBlock the callback to send after the webpageURL is called
574574
575-
@discussion passes the string of the redirected URL to the callback
576-
*/+(void) getAndTrackDeeplink:(NSURL *)webpageURL callbackBlock:(ITEActionBlock)callbackBlock;
575+
@discussion passes the string of the redirected URL to the callback, returns the original webpageURL if not an iterable link
576+
*/
577+
+(void) getAndTrackDeeplink:(NSURL *)webpageURL callbackBlock:(ITEActionBlock)callbackBlock;
577578

578579
@end
579580

Iterable-iOS-SDK/IterableAPI.m

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
#import "IterableNotificationMetadata.h"
2323
#import "IterableInAppManager.h"
2424
#import "IterableConstants.h"
25+
#import "IterableDeeplinkManager.h"
2526

26-
@interface IterableAPI () {
27-
}
28-
27+
@interface IterableAPI ()
2928
@end
3029

3130
@implementation IterableAPI {
@@ -414,19 +413,8 @@ + (IterableAPI *)sharedInstanceWithApiKey:(NSString *)apiKey andUserId:(NSString
414413
// documented in IterableAPI.h
415414
+(void) getAndTrackDeeplink:(NSURL *)webpageURL callbackBlock:(ITEActionBlock)callbackBlock
416415
{
417-
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:ITBL_DEEPLINK_IDENTIFIER options:0 error:NULL];
418-
NSString *urlString = webpageURL.absoluteString;
419-
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
420-
421-
if (match == NULL) {
422-
callbackBlock(webpageURL.absoluteString);
423-
} else {
424-
NSURLSessionDataTask *trackAndRedirectTask = [[NSURLSession sharedSession]
425-
dataTaskWithURL:webpageURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
426-
callbackBlock(response.URL.absoluteString);
427-
}];
428-
[trackAndRedirectTask resume];
429-
}
416+
IterableDeeplinkManager *deeplinkManager = [IterableDeeplinkManager instance];
417+
[deeplinkManager getAndTrackDeeplink:webpageURL callbackBlock:callbackBlock];
430418
}
431419

432420
// documented in IterableAPI.h
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// IterableDeeplink.h
3+
// Pods
4+
//
5+
// Created by David Truong on 11/16/17.
6+
//
7+
//
8+
9+
10+
#import "IterableConstants.h"
11+
12+
@interface IterableDeeplinkManager : NSObject
13+
14+
/**
15+
@method
16+
17+
@abstract a singleton of IterableDeeplinkManager
18+
*/
19+
+(instancetype)instance;
20+
21+
/*!
22+
@method
23+
24+
@abstract tracks a link click and passes the redirected URL to the callback
25+
26+
@param webpageURL the URL that was clicked
27+
@param callbackBlock the callback to send after the webpageURL is called
28+
29+
@discussion passes the string of the redirected URL to the callback
30+
*/
31+
-(void) getAndTrackDeeplink:(NSURL *)webpageURL callbackBlock:(ITEActionBlock)callbackBlock;
32+
33+
@end
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//
2+
// IterableDeeplink.m
3+
// Pods
4+
//
5+
// Created by David Truong on 11/16/17.
6+
//
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "IterableDeeplinkManager.h"
11+
#import "IterableConstants.h"
12+
13+
@interface IterableDeeplinkManager () <NSURLSessionDelegate>
14+
@end
15+
16+
@implementation IterableDeeplinkManager {
17+
}
18+
19+
// the URL session we're going to be using
20+
static IterableDeeplinkManager *deeplinkManager;
21+
NSURLSession *redirectUrlSession;
22+
NSString *deepLinkLocation;
23+
24+
// documented in IterableDeeplinkManager.h
25+
+(instancetype)instance
26+
{
27+
if (deeplinkManager == nil) {
28+
deeplinkManager = [[IterableDeeplinkManager alloc] init];
29+
}
30+
return deeplinkManager;
31+
}
32+
33+
/**
34+
@method
35+
36+
@abstract creates an instance of IterableDeeplinkManager and redirectUrlSession
37+
*/
38+
- (instancetype)init {
39+
self = [super init];
40+
[self createRedirectUrlSession];
41+
return self;
42+
}
43+
44+
// documented in IterableDeeplinkManager.h
45+
-(void)getAndTrackDeeplink:(NSURL *)webpageURL callbackBlock:(ITEActionBlock)callbackBlock
46+
{
47+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:ITBL_DEEPLINK_IDENTIFIER options:0 error:NULL];
48+
NSString *urlString = webpageURL.absoluteString;
49+
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
50+
51+
if (match == NULL) {
52+
callbackBlock(webpageURL.absoluteString);
53+
} else {
54+
NSURLSessionDataTask *trackAndRedirectTask = [redirectUrlSession
55+
dataTaskWithURL:webpageURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
56+
callbackBlock(deepLinkLocation);
57+
}];
58+
[trackAndRedirectTask resume];
59+
}
60+
}
61+
62+
/**
63+
@method
64+
65+
@abstract creates a singleton URLSession for the class to use
66+
*/
67+
- (void)createRedirectUrlSession
68+
{
69+
static dispatch_once_t onceToken;
70+
dispatch_once(&onceToken, ^{
71+
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
72+
redirectUrlSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
73+
});
74+
}
75+
76+
77+
//////////////////////////////////////////////////////////////
78+
/// @name NSURLSessionDelegate Functions
79+
//////////////////////////////////////////////////////////////
80+
81+
/**
82+
@method
83+
84+
@param session the session
85+
@param task the task
86+
@param redirectResponse the redirectResponse
87+
@param request the request
88+
@param completionHandler the completionHandler
89+
90+
@abstract delegate handler when a redirect occurs. Stores a reference to the redirect url and does not execute the redirect.
91+
*/
92+
- (void)URLSession:(NSURLSession *)session
93+
task:(NSURLSessionTask *)task
94+
willPerformHTTPRedirection:(NSHTTPURLResponse *)redirectResponse
95+
newRequest:(NSURLRequest *)request
96+
completionHandler:(void (^)(NSURLRequest *))completionHandler
97+
{
98+
deepLinkLocation = request.URL.absoluteString;
99+
completionHandler(nil);
100+
}
101+
102+
@end

0 commit comments

Comments
 (0)