I have a textbox which I can type in an url in to. The problem is if I just type in 'cnn.com' or any site without 'http://' in front, the page does not load. Is there a way to fix this?
I searched the apple documentation but I could not locate the help I was looking for.
My code:
NSString *zzz = URLbox.text;
NSURL *url = [NSURL URL开发者_如何学PythonWithString:zzz];
[webBrowser loadRequest:[NSURLRequest requestWithURL:url]];
Check if the input URL (URLbox.text) starts with either http://
or https://
and if it doesn't, prefix it with http://
.
A URL include a scheme, unless it's relative as part of a document. cnn.com
is a casual abbreviation for http://cnn.com
or http://www.cnn.com
. Browsers normally add http://
automatically to what you type in the location bar before submitting the request.
You should check that whether the user's string starts with a scheme (not necessarily just http://
or https://
, but that might be sufficient in your use-case) and add one by default if it's missing.
精彩评论