uiwebview在我更改地址栏中的内容后不起作用?

问题描述 投票:0回答:2

当我在网络视图地址栏中输入https://en.wikipedia.org/wiki/united_kingdom时,它可以工作,但是如果输入https://en.wikipedia.org/wiki/united王国,则不能...

有人可以向我解释为什么会这样吗?

ios uiwebview
2个回答
1
投票

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!
}

0
投票

浏览器不允许空格。网页浏览器(桌面),例如chrome(即chrome)等,会自动将空格转换为百分号%。要在目标c上执行此操作,您需要使用stringByAddingPercentEscapesUsingEncoding方法对其进行封装。下面是一个工作示例。

NSString *myUrl = @"http://en.m.wikipedia.org/wiki/united kingdom";
[myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
© www.soinside.com 2019 - 2024. All rights reserved.