【问题标题】:How to Load large HTML string in WebView with LoadHTMLString method?如何使用 LoadHTMLString 方法在 WebView 中加载大的 HTML 字符串?
【发布时间】:2014-11-07 10:47:53
【问题描述】:

我必须使用 UIWebView 的“LoadHTMLString”方法将 html 字符串加载到 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 是空的,它没有显示任何东西。

当我将此代码放入扩展名为“html”的文件中并加载到 webview 时,它工作正常。

谁能帮我把它加载到 WebView 上?或者谁能​​告诉我这段代码出了什么问题。

【问题讨论】:

  • 它不工作的原因是因为你的字符串不正确。如果我将该字符串放入一个简单的 html 文档并尝试加载它,它会通知我存在语法错误:“[Error] SyntaxError: Unexpected keyword 'var'. Expected ';'在 var 声明之后。" 重新评估您尝试加载的字符串并使其在项目之外解析。

标签: javascript html ios objective-c uiwebview


【解决方案1】:

您只需要创建一个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文件时,我们需要将这些文件添加到“Copy Bundle Resources”中。以便在运行时加载 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>];

【讨论】:

  • 他在通过字符串询问!不是文件
  • @srinivasn - 哦,是的,这被忽略了。
  • 是的,我正在寻找通过字符串加载。因为它有经纬度坐标。我想根据应用程序中位置的选择来更改它们。这就是我想要它作为字符串的原因。如果我将其放入文件并复制到捆绑资源,则无法做到这一点。如果有任何替代方案,请建议我。
  • 是的,你可以用文件来做。我会给你一个解决方案。
  • 感谢您的详尽回答@Kampai,您能告诉我您的回答中的“htmlString”是什么吗?
【解决方案2】:

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

斯威夫特

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,对其进行转换,替换标签“#latitude#”并使用目录加载 HTML(用于加载 JS 等)。

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 2019-08-13
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多