Load HTML from an URL

Discussion in 'iOS Development' started by sKeLeTr0n, Oct 23, 2009.

  1. sKeLeTr0n

    sKeLeTr0n New Member

    Joined:
    Mar 9, 2008
    Messages:
    10
    Likes Received:
    0
    Device:
    iPod touch
    Hello,
    I retrieving an HTML code from a Website that is loaded on my Webview, I have no problems with that. But is it possible to get the HTML directly from an URL so that I dont have to use the Webview? How would it look like?
    I appriciate your help

    Please Register or Log in to view images


    sKeLeTr0n

    Code:
    NSString *urlAddress = @"http://www.google.com/";
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];
    	
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    	
    //Load the request in the UIWebView.
    [webView loadRequest:requestObj];
    
    
    //Retrieve the HTML Code
    NSString *htmltext = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
    
    //Display the HTML Code in the TextView
    textView.text = htmltext;
    
  2. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    You would still need the webview to parse and obey the HTML Markup. If you just want to retrieve the text itself, use a .txt file. If you want to actually execute the HTML commands, you will need the UIWebView
  3. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I'm assuming your trying to display the html code itself.

    The solution is quite simple:

    [OBJC]
    NSString *htmlCode = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString

    Please Register or Log in to view images

    "http://ifans.com"]];
    [/OBJC]
  4. sKeLeTr0n

    sKeLeTr0n New Member

    Joined:
    Mar 9, 2008
    Messages:
    10
    Likes Received:
    0
    Device:
    iPod touch
    thank you Axis that really helped!
    But what if I now want to get "document.getElementsByTagName('table')[0].innerHtml" from that html code?
    When I use the WebView I do:
    Code:
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('table')[0].innerHTML"];
    
    But how can i search for the Elements "table" in the html code that I got?

Share This Page