-1

I'm trying to programme a mini browser in Xcode however at the moment the UIWebView will only load URLs that include the http ://www The user submits their URL using a UITextField and the contents become a string.

I wondered if there was a way to either search the submitted string and add the http or www or both where required or format the text input so it automatically checks to see if the correct address is used.

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mark
  • 131
  • 1
  • 3

1 Answers1

2

Do something like this:

NSString *urlString = ... // the user entered URL string
if (![urlString hasPrefix:@"http://"]) {
    urlString = [@"http://" stringByAppendingString:urlString];
}

Note that this is just a rough suggestion to get you started. This code doesn't handle cases such as the URL already having a prefix of "https://" or typos such as "htp://".

A better approach might be:

NSURL *url = [NSURL URLWithString:urlString];
NSString *scheme = [url scheme];
if (scheme.length == 0) {
    // The string has no scheme - add "http://"
} else {
    // check for valid schemes
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thank you/ Could you explain your second suggestion a little bit more? I'm quite new to this. I guess if you had the http but not the www you'd have to add the www in at a specific location? – Mark Jun 27 '13 at 19:11
  • There is no need for "www" protocol. Did you find any specific solution? to get string like safari? – Salih Ozdemir Apr 01 '14 at 23:00