Parsing hidden WebView items.

Discussion in 'iOS Programming' started by mycompuser, Aug 1, 2013.

  1. mycompuser macrumors member

    May 8, 2012
    Hi All,

    I am in the process of developing an feature in MAC which is already developed on iOS.

    In MAC, I load an web page on a WebView which contains some fields where I need to provide my credetials to authenticate and after successful authentication, the webpage gives me some response which I need to parse for it's data.

    Below is the format of the data sent by the server on my iPhone
    	<meta name="HandheldFriendly" content="True">
    	<meta name="viewport" content="width=device-width, initial-scale=1,,user-scalable=no">
    	<meta http-equiv="cleartype" content="on">
    	<title>Getting Started</title>
    	<link rel="stylesheet" href="css/style.css">
    	<script type="text/javascript" async="" src=""></script><script type="text/javascript">
    		var _gaq = _gaq || [];
    		_gaq.push([ '_setDomainName', 'none' ]);
    		(function() {
    			var ga = document.createElement('script');
    			ga.type = 'text/javascript';
    			ga.async = true;
    			ga.src = ('https:' == document.location.protocol ? 'https://ssl'
    					: 'http://www')
    					+ '';
    			var s = document.getElementsByTagName('script')[0];
    			s.parentNode.insertBefore(ga, s);
    <body class="background">
    		<div class="boxcenter">
    			<div id="pushme_2"></div>
    			<div id="pushme_15"></div>
    			<p class="label-text">
    				Almost done.
    			<input id="encryptedData" type="hidden" value="InPU4j5rmkC/eQwlInfSa54cOlnRQRiIQMjlFZ0QJQbsuUhuEAAUnUqmLFLKoyRGzBB3Z8lEcRPbNQk5f6Zrjmp+S/96xU++URuYP1N0oQ03V2dGWJ=">
    			<input id="decryptKey" type="hidden" value="CDtMmcVcXJhp5fcrW5yQqrTdAPZiYPtjQx9+">
    The code that I used on iPhone to print contents in html for debugging is provided below.
        - (void)webViewDidFinishLoad:(UIWebView *)webView
        NSString *test = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
        NSLog(@"%@", test);
    The problem that I am currently facing in MAC platform is that I am not able to parse the contents of the "decryptKey" key which is defined as of type 'hidden'.

    I use the below mentiond code to print the contents of WebView on MAC and find that it does not print the tags whose type is declared as "hidden".

        NSLog(@"In Mac: %@", [(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] outerText]);
    I am not sure if the code to display the contents of UIWebView and WebView are equivalent (if not equivalent, it explains why the hidden types are not displayed).

    Below is the code snippet on MAC that I use to parse the fields where I am facing some issue.

    - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
            id win = [webView windowScriptObject];//webView is the WebView object.        
            id location = [win valueForKey:@"encryptedData"];
            NSString *activationString = [location valueForKey:@"value"];//Returns valid string.
              id win2 = [webView windowScriptObject];
             id keyLocation = [win2 valueForKey:@"decryptKey"];
                NSString *decryptionKey = [keyLocation valueForKey:@"value"];//does not return the string.
    Not sure why I am able to parse encryptedData tag but not decryptKey tag.

    Am I doing something wrong with respect to parsing of WebView data?
  2. mycompuser thread starter macrumors member

    May 8, 2012
    Figured it out myself.

    On MAC, I had to use the below line to print the contents sent by server.

     NSLog(@"%@", [(DOMHTMLElement *)[[[webView mainFrame] DOMDocument] documentElement] innerHTML]);
    And for some reason, the "decryptKey"'s value is empty hence I was not able to get the value.

Share This Page