When I 开发者_StackOverflow中文版type https://en.wikipedia.org/wiki/united_kingdom in webview address bar it works, but if type https://en.wikipedia.org/wiki/united kingdom it does not...
Can anyone explain to me why this is happening?
You cannot have spaces in a url, thats why its not working!
Edit: To avoid having spaces in your url, you can check if the url has got any spaces, and if, show an alert.
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!
}
Browsers does not allow blanks spaces.
Web Browser (desktop) like chrome, ie etc automatically convert blanks spaces to percent sign %. To do this on objective c you need to encapsulate it with stringByAddingPercentEscapesUsingEncoding
method. Below is a working example.
NSString *myUrl = @"http://en.m.wikipedia.org/wiki/united kingdom";
[myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
精彩评论