How to Load large HTML string in WebView with LoadHTMLString method?

十年热恋 提交于 2019-12-02 07:57:30

问题


I have to load an html string onto the UIWebView with 'LoadHTMLString' method of UIWebView.

Please find the below code.

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

When I load this, my webview is empty, it is not showing anything.

When I put this code into file with the extension 'html' and load on to the webview it is working fine.

Can any one help me in loading this on to the WebView? Or could any one let me know what is going wrong in this code.


回答1:


You just need to create one html file and put all html content in it, And use code to load that file

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

And using this also

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

Edit:

I was overlooked that you have mention that loading file is working perfect.

I think issue is because of you load this string at runtime.

We all know that when we use html and css files we need to add these files in "Copy Bundle Resources". So that it will be available when html page load at runtime.

So may be issue here is some of the functions in that html string are use some file or assistance before it loads. So that's why runtime html creation and loading is not working.

How ever more of clear me if i'm misleading.

You can set runtime values in html file though.

Exmaple.html

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

<body>

#latitude# 

#longitude# 

</body>
<html>

See above file, You can add marker like #latitude# and #longitude# in that file. And replace this at a runtime. Like below code.

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



回答2:


After two days, long sleep, I've come up with this.

Swift

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"))

First, my HTML code is in my project ( public_html/index.html ). This code loads the HTML, converts it, replaces the tag '#latitude#' and loads the HTML using the directory ( for loading JS, etc.).



来源:https://stackoverflow.com/questions/26799493/how-to-load-large-html-string-in-webview-with-loadhtmlstring-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!