如何更改UIWebView中的字符编码? [英] How to change the character encoding in UIWebView?
问题描述
问题摘要:当浏览在iOS上未使用UIWebView明确指定正确字符编码的非英语网站时,无法正确显示该网页。
详细说明:由于UIWebView中的 loadRequest:
方法将使用指定的编码在从Web服务器发送的字符集头或在HTML元标签中写入的字符集,并且默认为iso-8859-1(好,我不太确定),当charset没有指定,这导致非英语网站无法正常显示。
我尝试过改变UIWebView使用的字符集的方式,但唯一的方法是使用 loadData:MIMEType:
但是,使用 loadData不是一个好主意:MIMEType:textEncodingName:baseURL:
+ NSURLConnection而不是 loadRequest:
,因为UIWebView不会调用委托方法 webView :shouldStartLoadWithRequest:navigationType:
对于框架,即使我们发现一个方法来获得通知当UIWebView加载框架,我们不能调用 loadData:MIMEType:textEncodingName:baseURL: code>加载框架内容,因为它将加载框架作为外部页面,如果我们这样做。
此外,我试图使用javascript黑客,但似乎该属性是只读的,不能更改。
[webView stringByEvaluatingJavaScriptFromString:@document.characterSet ='utf-8';];
另一种解决方法是将元标记插入HTML,并要求UIWebView加载修改后的代码,上述框架问题也适用于此。
< meta http-equiv =Content-Typecontent = html; charset = utf-8>
问题:有更好的解决方案,可以更改字符编码在UIWebView中加载的网页,并正确处理框架?
您可以通过手动加载HTML,然后将修改的内容加载到UIWebView中。
- 将不包含元标记的页面中的HTML手动加载到字符串中(例如使用
NSURLConnection
) - 使用字符串操作,将适当的编码元标记插入手动加载的HTML
- 使用
loadHTMLString:
- 实施UIWebView委托
- 实施方法 webView:shouldStartLoadWithRequest:navigationType:
- 在委托方法中,手动加载网址并修改内容,然后将其设置到UIWebView中,如上所示。
将您网络视图中的HTML设置为修改的字符串。
现在,这将适用于不包含链接的网页。如果它包含链接,那么你会发现,他们点击链接后,新页面将不会有您的修改。因此,您需要手动拦截所有点击。
Summary of the problem: When browsing non-English sites that does not explicitly specify the correct character encoding with UIWebView on the iOS, the page cannot be displayed correctly.
Detailed explanation: As the loadRequest:
method in UIWebView will use the encoding specified in the charset header sent from the web server or the charset written in the HTML meta tag, and default to iso-8859-1 (well, I am not too sure about this) when charset is not specified, which cause non-English sites cannot display properly.
I have tried to Google for a way to change the charset that the UIWebView use, but the only way is to use the loadData:MIMEType:textEncodingName:baseURL:
method to specify the encoding name.
However, it is not a good idea to use loadData:MIMEType:textEncodingName:baseURL:
+ NSURLConnection instead of loadRequest:
, because UIWebView won't call the delegate method webView:shouldStartLoadWithRequest:navigationType:
for frames, and even if we found a way to get notified when UIWebView load a frame, we cannot call loadData:MIMEType:textEncodingName:baseURL:
to load the frame content, because it will load the frame as the outer page if we do that.
Besides, I have tried to use a javascript hack, but seems that property is read-only and cannot be changed.
[webView stringByEvaluatingJavaScriptFromString:@"document.characterSet='utf-8';"];
Another workaround is inserting a meta tag to the HTML, and ask UIWebView to load the modified code, but the frame problem mentioned above also apply here.
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
Question: Is there any better solution that can change the character encoding in a loaded webpage in UIWebView, and handle frames properly?
You can do this by manually loading the HTML, modifying it, and then loading the modified content into the UIWebView.
- manually load the HTML from the page that doesn't include the meta tag, into a string (e.g. use
NSURLConnection
) - using string manipulation, insert your appropriate encoding meta tag into the manually loaded HTML
- set the HTML in your web view to the modified string using
loadHTMLString:
Now, this will work fine for a web page that contains no links. If it contains links, then you will find that after they click on a link, the new page will not have your modification in place. Therefore, you will need to manually intercept all of the clicks. Here's how you can do that:
- Implement a UIWebView delegate
- Implement the method webView:shouldStartLoadWithRequest:navigationType:
- In your delegate method, load the URL manually and modify the content before setting it into the UIWebView, as above.
这篇关于如何更改UIWebView中的字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!