Created
May 8, 2012 21:45
-
-
Save daveanderson/2639625 to your computer and use it in GitHub Desktop.
some crazy web view manipulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (void)presentCustomizedCalgaryParkingSiteFromViewController:(UIViewController *)viewController { | |
NSString *registerURL = @"https://www.calgaryparking.com/web/guest/cpa_secure/parkplus?p_p_id=2&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-3&p_p_col_count=3&_2_struts_action=%2Fmy_account%2Fcreate_account"; | |
WebViewController *webViewController = [[WebViewController alloc] initWithURLString:registerURL]; | |
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:webViewController]; | |
NSString *registerRecognitionSnippet = @"_2_struts_action=%2Fmy_account%2Fcreate_account"; | |
NSString *registerSuccessRecognitionSnippet = @"p_l_id="; | |
NSString *registerSuccessUsernameSnippet = @"login="; | |
NSString *loginPageSnippet = @"https://www.calgaryparking.com/c/portal/change_password"; | |
// give this web view some blocks for handling when the form is submitted and when a url is loaded... | |
webViewController.formSubmittedHandler = ^(UIWebView *webView, NSURLRequest *request){ | |
// look for action _2_struts_action | |
NSString *absoluteString = request.URL.absoluteString; | |
//NSString *parameterString = request.URL.parameterString; | |
NSString *query = request.URL.query; | |
//NSString *data = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]; | |
// NSLog(absoluteString); | |
// NSLog(parameterString); | |
// NSLog(query); | |
// NSLog(data); | |
// basic registration is being submitted | |
if (query && [query rangeOfString:registerRecognitionSnippet].location != NSNotFound) { | |
NSString *username = [webView stringByEvaluatingJavaScriptFromString:@"document._2_fm._2_screenName.value"]; | |
NSLog(@"Registering username: %@", username); | |
// could also get this from the post data? | |
} | |
// succeeded in registering - have a temporary password, need to enter it to change to the real password | |
else if (query && [query rangeOfString:registerSuccessRecognitionSnippet].location != NSNotFound && | |
[query rangeOfString:registerSuccessUsernameSnippet].location != NSNotFound) { | |
NSRange loginSnippetRange = [query rangeOfString:registerSuccessUsernameSnippet]; | |
NSString *username = [query substringFromIndex:(loginSnippetRange.location + loginSnippetRange.length)]; | |
NSLog(@"Registered username: %@", username); | |
// now that we've registered we need to wait for this page to load and then get our temporary password | |
webViewController.finishLoadHandler = ^(UIWebView *webView, NSURLRequest *request) { | |
NSString *password = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('tabs1already-registeredTabsSection').getElementsByClassName('portlet-msg-success')[0].getElementsByTagName('b')[0].innerHTML;"]; // this is the temporary password | |
// NSLog(@"temporary password: %@", password); | |
// insert the temporary password into the password field | |
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('password').value = '%@';", password]]; | |
webViewController.finishLoadHandler = nil; | |
}; | |
} | |
// posting our real password | |
else if ([absoluteString isEqualToString:loginPageSnippet] && [request.HTTPMethod isEqualToString:@"POST"]) { | |
// check if this wants us to change our password | |
NSString *passwordSubmissionParameters = [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]; | |
NSRange password1Range = [passwordSubmissionParameters rangeOfString:@"&password1="]; | |
NSRange password2Range = [passwordSubmissionParameters rangeOfString:@"&password2="]; | |
if (password1Range.location != NSNotFound && password2Range.location != NSNotFound) { | |
NSString *password1 = [passwordSubmissionParameters substringWithRange:NSMakeRange(password1Range.location + password1Range.length, password2Range.location - (password1Range.location + password1Range.length))]; | |
NSString *password2 = [passwordSubmissionParameters substringFromIndex:password2Range.location + password2Range.length]; | |
if ([password1 isEqualToString:password2]) { | |
NSLog(@"Real password: %@", password1); | |
} | |
} | |
} | |
// else { | |
// NSLog(@"Unhandled form url: %@", request.URL.absoluteString); | |
// } | |
}; | |
[viewController presentModalViewController:navController animated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment