如何使用LoadHTMLString方法在WebView中加载大型HTML字符串?

时间:2014-11-07 10:47:53

标签: javascript html ios objective-c uiwebview

我必须使用' LoadHTMLString'将一个html字符串加载到UIWebView上。 UIWebView的方法。

请找到以下代码。

[wv loadHTMLString:[NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\"><meta charset=\"utf-8\"><title>Traffic layer</title><style>html, body, #map-canvas {height: 100%%;margin: 0px;padding: 0px}</style><script src=\"https://maps.googleapis.com/maps/api/js?v=3.exp\"></script><script>function initialize() {var myLatlng = new google.maps.LatLng(34.04924594193164, -118.24104309082031);var mapOptions = {zoom: 13,center: myLatlng}var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptiotrafficLayer.setMap(map);}google.maps.event.addDomListener(window, 'load', initialize);</script></head><body><div id=\"map-canvas\"></div></body></html>"] baseURL:nil]

当我加载它时,我的webview是空的,它没有显示任何内容。

当我将此代码放入带有扩展程序&#39; html&#39;的文件中时并加载到webview它工作正常。

任何人都可以帮我加载到WebView吗?或者任何人都可以告诉我这段代码出了什么问题。

2 个答案:

答案 0 :(得分:2)

您只需创建一个html文件并将所有html内容放入其中,并使用代码加载该文件

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"] isDirectory:NO]]];

并使用此

NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

[self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];

修改

我忽略了你提到加载文件工作正常。

我认为问题是因为你在运行时加载了这个字符串。

我们都知道,当我们使用html和css文件时,我们需要在&#34;复制捆绑资源&#34; 中添加这些文件。这样在运行时加载html页面时它就可用了。

因此可能存在的问题是,html字符串中的一些函数在加载之前使用了一些文件或帮助。这就是为什么运行时html创建和加载不起作用的原因。

如果我有误导性,我会更清楚。

您可以在html文件中设置运行时值。

<强> Exmaple.html

<!DOCTYPE html>
<html>
<head>
</head>

<body>

#latitude# 

#longitude# 

</body>
<html>

参见上述文件,您可以在该文件中添加#latitude##longitude#等标记。并在运行时替换它。如下面的代码。

NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

html = [html stringByReplacingOccurrencesOfString:@"#latitude#" withString:<LATITUDE-VALUE>];
html = [html stringByReplacingOccurrencesOfString:@"#longitude#" withString:<LONGITUDE-VALUE>];

答案 1 :(得分:1)

经过两天长时间的睡眠,我已经想出了这个。

斯威夫特

let path = NSBundle.mainBundle().pathForResource("public_html/index", ofType: "html")
var html = try? NSString(contentsOfFile: path! as String, encoding: NSUTF8StringEncoding)
html = html?.stringByReplacingOccurrencesOfString("#latitude#", withString:"<p class=\"text-center\">Center aligned text.</p><blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p></blockquote>")
webView.loadHTMLString(html as! String, baseURL: NSURL(fileReferenceLiteral: "public_html"))

首先,我的HTML代码在我的项目中(public_html / index.html)。此代码加载HTML,转换它,替换标记&#39; #latitude#&#39;并使用目录加载HTML(用于加载JS等)。