15

I'm new to iPhone development and Objective-C. Using the ZBar SDK I've developed a basic app which scans a QR image from the photo album and outputs what it translates to.

I want to know if there is a way to take this output, determine whether it is a URL and if so open it in a web browser.

Zachary Markham
  • 361
  • 2
  • 5
  • 14

3 Answers3

37

NSURL's URLWithString returns nil if the URL passed is not valid. So, you can just check the return value to determine if the URL is valid.

UPDATE

Just using URLWithString: will usually not be enough, you probably also want to check if the url has a scheme and a host, otherwise, urls such as al:/dsfhkgdsk will pass the test.

So you probably want to do something like this:

NSURL *url = [NSURL URLWithString:yourUrlString];
if (url && url.scheme && url.host)
{
   //the url looks ok, do something with it
   NSLog(@"%@ is a valid URL", yourUrlString);
}

If you only want to accept http URLs you may want to add [url.scheme isEqualToString:@"http"].

Daniel
  • 20,420
  • 10
  • 92
  • 149
  • Thanks for pointing me in the right direction :) Got it sorted now, need a lot more practice with Objective C though! haha – Zachary Markham Sep 28 '10 at 11:24
  • 3
    It seems to have a loose definition what's a valid URL. "foo.com" will parse so you probably want to check that the resulting object has a scheme property. – drewish Dec 12 '11 at 14:19
  • 3
    you can't just trust URLWithString, because NSURL *url = [NSURL URLWithString:@"a"]; will return a to you. see http://stackoverflow.com/questions/1471201/how-to-validate-an-url-on-the-iphone – 6 1 Jun 05 '13 at 14:32
  • However, you can use [this](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) if you want to use regex instead. – Daniel Jun 05 '13 at 14:54
  • I mean this " you can just check the return value to determine if the URL is valid." statement is not enough. – 6 1 Jun 06 '13 at 05:41
  • Is it OK to skip the `host` check if we also accept local file URLs? – adib Dec 05 '15 at 03:19
  • you should check the `length` of the scheme since string "a" returns an empty string for it's scheme. – adib Dec 05 '15 at 03:21
3

Here is an alternative solution which I came across. You can do like this.

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourstring"]];
bool valid = [NSURLConnection canHandleRequest:req];

Source : https://stackoverflow.com/a/13650542/2513947

Community
  • 1
  • 1
Rizwan Ahmed
  • 919
  • 13
  • 19
-1

You can look at below code to check whether a string contains website url or simple text.

 NSString *getString =[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"];
    if([getString rangeOfString:@"Www"].location == NSNotFound
       && [getString rangeOfString:@"www"].location == NSNotFound && [getString rangeOfString:@"com"].location == NSNotFound)
    {
        [companyNameTF setText:[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"]];
    }
    else
    {
         [websiteTF setText:[[NSUserDefaults standardUserDefaults]valueForKey:@"searchKey"]];
    }
Yash
  • 171
  • 2
  • 8
  • This works in very limited cases. I.e. only urls that include `www` and `com`, which is by no means all of them. It only works if you know what to expect the URL to look like. – Jake T. Jul 05 '18 at 16:56