开发者

nsurl doesn't load sites if http or https not include

开发者 https://www.devze.com 2023-03-08 21:40 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号