Search code examples
iosuiwebviewxcode4

UIWebView showing the URL instead of webpage


I try to load a URL(in string format) to a UIWebView, but instead of showing the webpage, it shows the URL. Here's my code:

URL = @"www.Google.com" 
NSString *path = [[NSBundle mainBundle] bundlePath]; 
baseURL = [NSURL fileURLWithPath:path];
[webView clearsContextBeforeDrawing];   
[webView loadHTMLString:URL baseURL:baseURL];
[webView setNeedsLayout];

Solution

  • loadHTMLString:baseURL: takes the html page content as input and not just URL in string form. It treats www.Google.com as a html string and it gets shown. You will have to do,

    NSString * htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"] encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlString baseURL:nil];