如果我在地址栏中改变一些东西,Uiwebview不工作



当我在webview地址栏中输入https://en.wikipedia.org/wiki/united_kingdom它工作,但如果输入https://en.wikipedia.org/wiki/united王国它不…

谁能给我解释一下为什么会这样?

url不能有空格,这就是为什么它不能工作!

编辑:为了避免在你的url中出现空格,你可以检查url是否有空格,如果有,显示一个警告。

if([url.text rangeOfString:@" "].location != NSNotFound) {
    //url contains a space, show an alert and don't load the request here!
}
else {
    //load the request into your webview!
}

浏览器不支持空格。Web浏览器(桌面),如chrome, ie等自动将空格转换为百分号%。要在objective c上做到这一点,你需要用stringByAddingPercentEscapesUsingEncoding方法封装它。下面是一个工作示例:

NSString *myUrl = @"http://en.m.wikipedia.org/wiki/united kingdom";
[myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

最新更新