如何在iPhone的WebView中显示HTML数据 [英] How to display html data in webview in iphone

查看:55
本文介绍了如何在iPhone的WebView中显示HTML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Webview中显示一些html内容数据.为了显示html数据,我有一个html文件,但是我不知道如何在webview中执行该html.如果有人知道,请指导我.

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.

预先感谢

推荐答案

这是一个太简单的代码,使用它很快就会受到限制.
例如,如果您要显示来自项目捆绑包的图像.

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.

首选(如果使用HTML文件):

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

或更短以用于直接HTML内联编码:

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

这篇关于如何在iPhone的WebView中显示HTML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆