Viken Ong
Viken Ong

Reputation: 109

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];

Upvotes: 0

Views: 517

Answers (3)

Mullins
Mullins

Reputation: 2364

You have loaded the content of the UIWebView with the string 'www.Google.com' not the content from that address. loadHTMLString will load HTML content from a string.

You need to use loadRequest.

See http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWebView_Class/Reference/Reference.html

Upvotes: 1

petershine
petershine

Reputation: 3200

Use loadRequest instead for www.Google.com

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"www.google.com"]];
[webView loadRequest:request];

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

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];

Upvotes: 1

Related Questions