当我在网络视图地址栏中输入https://en.wikipedia.org/wiki/united_kingdom时,它可以工作,但是如果输入https://en.wikipedia.org/wiki/united王国,则不能...
有人可以向我解释为什么会这样吗?
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!
}
浏览器不允许空格。网页浏览器(桌面),例如chrome(即chrome)等,会自动将空格转换为百分号%。要在目标c上执行此操作,您需要使用stringByAddingPercentEscapesUsingEncoding
方法对其进行封装。下面是一个工作示例。
NSString *myUrl = @"http://en.m.wikipedia.org/wiki/united kingdom";
[myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];