Nari
Nari

Reputation: 213

How to display html data in webview in iphone

I want to display some html content data in webview. For displaying html data, i have one html file, but i dont know how to execute that html in webview. Please guide me if any one knows.

Thanking in advance

Upvotes: 1

Views: 3041

Answers (2)

Oliver
Oliver

Reputation: 23510

This is a too simple code and you will be limited very soon using it.
For example if you want to display images coming from your project bundle.

Prefer (if you use a HTML file) :

NSStringEncoding encoding;
NSError* error = nil;
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];

NSString* htmlPath = [[NSBundle mainBundle] pathForResource:self.htmlFilename ofType:@"htm"];
NSString* htmlContent = [NSString stringWithContentsOfFile:htmlPath usedEncoding:&encoding error:&error];
if (error != nil) // Manage the error

[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:bundlePath]];

or shorter for direct HTML inline coding :

NSString* htmlContent = @"<html><head><title></title></head><body><img src='image.jpg' /></body></html>";
[webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

Upvotes: 1

Simon Lee
Simon Lee

Reputation: 22334

Use the loadString method...

[webView loadHTMLString:myHtmlString baseURL:nil];

Upvotes: 4

Related Questions