Search code examples
objective-cnsurlnsurlrequest

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


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 URLWithString:zzz];
[webBrowser loadRequest:[NSURLRequest requestWithURL:url]];

Solution

  • Check if the input URL (URLbox.text) starts with either http:// or https:// and if it doesn't, prefix it with http://.