问题描述
在我的应用程序中,我正在使用Webview根据用户的首选语言加载网站的不同版本.为此使用WebFrame的loadRequest方法-
In my application, i'm using a Webview to load different versions of a website based on user's preferred language.Using WebFrame's loadRequest method for this -
[[aWebView主机] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aStr]]];
[[aWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aStr]]];
如果aStr仅包含英文字母,即aStr = http://....../language/English,则其正常工作&网页加载.但是,如果aStr是http://....../language/ウンウンウン(日语)之类的东西,则什么也不会发生,WebView不会加载,也不会引发任何错误.如果我将相同的链接粘贴到Safari中,则网页会加载到那里.关于如何解决此问题有什么建议吗?
If, aStr contains english alphabets only,i.e if aStr = http : // ....../language/English ,then its working fine & the webpage loads. But if aStr is something like http: //....../language/ウンウンウン(Japanese), nothing happens, neither WebView loads, nor it throws any error. If i paste the same link in Safari, the webpage loads there. Any suggestion on how to fix this issue?
我也尝试了[aWebView setMainFrameURL:aStr]方法.那里也有同样的问题.
I've also tried [aWebView setMainFrameURL:aStr] method. Same issue there too.
推荐答案
您必须先转义aStr
中的无效URL字符,然后才能使用它使用[aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
创建NSURL.
You have to escape the invalid URL characters in aStr
before you can use it to create the NSURL using [aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
.
//Encode invalid URL characters
aStr = [aStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
[[aWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aStr]]];
如果字符串无效,则
URLWithString:
返回nil值.这就是为什么您的UIWebView没有错误并且不加载任何内容的原因.
URLWithString:
returns a nil value if the string is invalid. Which is why you your UIWebView has no error and does not load anything.
这篇关于Webview loadRequest不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!