Skip to content

Instantly share code, notes, and snippets.

@mnbayan
Last active September 5, 2015 16:47
Show Gist options
  • Save mnbayan/07a0b30ab79bfb111abf to your computer and use it in GitHub Desktop.
Save mnbayan/07a0b30ab79bfb111abf to your computer and use it in GitHub Desktop.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *scheme = [[[request URL] scheme] lowercaseString];
if([scheme isEqualToString:@"optimizelygoal"]) {
// Pull the encoded JSON string out of the querystring
// Decode the string, convert JSON string into usable object
// Handle the message
// Return NO to prevent the webview from navigating
NSDictionary *userInfo = [self userInfoFromQuery:request.URL.absoluteString];
if (userInfo[@"name"] != nil){
//Perform tracking
//[Optimizely trackEvent:(NSString *)userInfo[@"name"]];
}
if (userInfo[@"revenue"] != nil){
//Perform tracking
//NSNumber *price = [self numberValue:(NSString *)userInfo[@"revenue"]];
//[Optimizely trackRevenue:price*100];
}
return NO;
}
// Allow normal webview navigation
return YES;
}
/// Obtains Key value pair from a given url string
///
/// :param: queryString The url query in NSString format
/// :returns: userInfo An instance of NSDictionay containing key-value pair obtained from the url query
- (NSDictionary *)userInfoFromQuery:(NSString *)queryString{
NSString* urlScheme = @"optimizelygoal://goal?";//Configurable
NSString* trimmedQueryString = [queryString stringByReplacingOccurrencesOfString:urlScheme withString:@""];
NSMutableDictionary* userInfo = [NSMutableDictionary new];
NSArray* components = [trimmedQueryString componentsSeparatedByString:@"&"];
for (int i = 0; i < components.count; i++) {
NSString *component = (NSString *)components[i];
NSArray* pair = [component componentsSeparatedByString:@"="];
if (pair.count != 2) continue;
NSString* key = pair[0];
NSString* value = pair[1];
userInfo[key] = value;
}
return userInfo;
}
/// Converts a valid string to NSNumber
///
/// :param: numString The string to be converted to NSNumber. This must contain only numbers and decimals.
/// :returns: The string in NSNumber
- (NSNumber *)numberValue:(NSString*)numString{
NSNumberFormatter *nf = [NSNumberFormatter new];
nf.numberStyle = NSNumberFormatterDecimalStyle;
return [nf numberFromString:numString];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment