Skip to content

Commit

Permalink
Added parse of url to utils class to reuse code.
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-clevertap committed Nov 22, 2024
1 parent f2a8b2c commit b9887c1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 53 deletions.
39 changes: 8 additions & 31 deletions CleverTapSDK/CTInAppDisplayViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,42 +315,19 @@ - (void)handleButtonClickFromIndex:(int)index {

- (void)triggerInAppAction:(CTNotificationAction *)action callToAction:(NSString *)callToAction buttonId:(NSString *)buttonId {
NSMutableDictionary *extras = [NSMutableDictionary new];
NSString *urlString = [action.actionURL absoluteString];

if (action.type == CTInAppActionTypeOpenURL) {
NSMutableDictionary *mutableParams = [NSMutableDictionary new];
NSString *urlString = [action.actionURL absoluteString];
NSURL *dl = [NSURL URLWithString:urlString];
NSMutableDictionary *mutableParams = [CTInAppUtils getParametersFromURL:urlString];

// Try to extract the parameters from the URL and overrite default dl if applicable
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
NSArray *comps = [urlString componentsSeparatedByString:@"?"];
if ([comps count] >= 2) {
// Extract the parameters and store in params dictionary
NSString *query = comps[1];
for (NSString *param in [query componentsSeparatedByString:@"&"]) {
NSArray *elts = [param componentsSeparatedByString:@"="];
if ([elts count] < 2) continue;
params[elts[0]] = [elts[1] stringByRemovingPercentEncoding];
};
mutableParams = [params mutableCopy];
if (mutableParams[@"params"]) {
extras = [mutableParams[@"params"] mutableCopy];

// Check for wzrk_c2a key, if present update its value after parsing with __dl__
NSString *c2a = params[CLTAP_PROP_WZRK_CTA];
if (c2a) {
c2a = [c2a stringByRemovingPercentEncoding];
NSArray *parts = [c2a componentsSeparatedByString:@"__dl__"];
if (parts && [parts count] == 2) {
dl = [NSURL URLWithString:parts[1]];
mutableParams[CLTAP_PROP_WZRK_CTA] = parts[0];

// Use the url from the callToAction param to update action
action = [[CTNotificationAction alloc] initWithOpenURL:dl];
}
// Use the url from the callToAction param to update action
if (mutableParams[@"deeplink"]) {
action = [[CTNotificationAction alloc] initWithOpenURL:mutableParams[@"deeplink"]];
}
}

if (mutableParams) {
extras = mutableParams;
}
}

// Added NSNull class check as we may receive callToAction value as NULL class
Expand Down
1 change: 1 addition & 0 deletions CleverTapSDK/CTInAppUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ typedef NS_ENUM(NSUInteger, CTInAppActionType){
+ (NSString * _Nonnull)inAppActionTypeString:(CTInAppActionType)type;
+ (NSBundle *_Nullable)bundle;
+ (NSString *_Nullable)getXibNameForControllerName:(NSString *_Nonnull)controllerName;
+ (NSMutableDictionary *_Nonnull)getParametersFromURL:(NSString *_Nonnull)url;

@end
32 changes: 32 additions & 0 deletions CleverTapSDK/CTInAppUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,36 @@ + (NSString *)getXibNameForControllerName:(NSString *)controllerName {
#endif
}

+ (NSMutableDictionary *)getParametersFromURL:(NSString *)urlString {
NSMutableDictionary *mutableParams = [NSMutableDictionary new];

// Try to extract the parameters from the URL and overrite default dl if applicable
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
NSArray *comps = [urlString componentsSeparatedByString:@"?"];
if ([comps count] >= 2) {
// Extract the parameters and store in params dictionary
NSString *query = comps[1];
for (NSString *param in [query componentsSeparatedByString:@"&"]) {
NSArray *elts = [param componentsSeparatedByString:@"="];
if ([elts count] < 2) continue;
params[elts[0]] = [elts[1] stringByRemovingPercentEncoding];
};

// Check for wzrk_c2a key, if present update its value after parsing with __dl__
NSString *c2a = params[CLTAP_PROP_WZRK_CTA];
if (c2a) {
c2a = [c2a stringByRemovingPercentEncoding];
NSArray *parts = [c2a componentsSeparatedByString:@"__dl__"];
if (parts && [parts count] == 2) {
params[CLTAP_PROP_WZRK_CTA] = parts[0];
mutableParams[@"deeplink"] = [NSURL URLWithString:parts[1]];
}
}

mutableParams[@"params"] = [params mutableCopy];
}

return mutableParams;
}

@end
28 changes: 6 additions & 22 deletions CleverTapSDK/InApps/CTInAppHTMLViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,34 +230,18 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
return;
}

NSMutableDictionary *mutableParams = [NSMutableDictionary new];
NSString *urlString = [navigationAction.request.URL absoluteString];
NSURL *dl = [NSURL URLWithString:urlString];
NSMutableDictionary *mutableParams = [CTInAppUtils getParametersFromURL:urlString];

// Try to extract the parameters from the URL and overrite default dl if applicable
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
NSArray *comps = [urlString componentsSeparatedByString:@"?"];
if ([comps count] >= 2) {
NSString *query = comps[1];
for (NSString *param in [query componentsSeparatedByString:@"&"]) {
NSArray *elts = [param componentsSeparatedByString:@"="];
if ([elts count] < 2) continue;
params[elts[0]] = [elts[1] stringByRemovingPercentEncoding];
};
mutableParams = [params mutableCopy];
NSString *c2a = params[CLTAP_PROP_WZRK_CTA];
if (c2a) {
c2a = [c2a stringByRemovingPercentEncoding];
NSArray *parts = [c2a componentsSeparatedByString:@"__dl__"];
if (parts && [parts count] == 2) {
dl = [NSURL URLWithString:parts[1]];
mutableParams[CLTAP_PROP_WZRK_CTA] = parts[0];
}
}
// Use the url from the callToAction param to update action
if (mutableParams[@"deeplink"]) {
dl = mutableParams[@"deeplink"];
}

if (self.delegate && [self.delegate respondsToSelector:@selector(handleNotificationAction:forNotification:withExtras:)]) {
CTNotificationAction *action = [[CTNotificationAction alloc] initWithOpenURL:dl];
[self.delegate handleNotificationAction:action forNotification:self.notification withExtras:mutableParams];
[self.delegate handleNotificationAction:action forNotification:self.notification withExtras:mutableParams[@"params"]];
}
[self hide:YES];
decisionHandler(WKNavigationActionPolicyCancel);
Expand Down

0 comments on commit b9887c1

Please sign in to comment.