spacesloth spacesloth - 1 month ago 8
iOS Question

objective c - How to tell if an NSURL is formatted as a phone number

I have an iOS app with a UITextView that auto detects links and phone numbers. What I want to do is intercept when a link is clicked using this:

-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange

and then I need to check whether the URL is a web link or a phone number.

Does anyone have any idea how to do this? The only way I can think of is to convert it to a string and check if it starts with @"tel" but there has to be a better way. Thanks!

Answer Source

There IS a better way.

An NSURL object has an attribute of scheme, which will identify what type of URL it is.

if ([URL.scheme isEqualToString:@"tel"]) {
     // handle telephone case here
} else {
    // handle http or other case here

The documentation says the scheme could be considered the protocol in most cases:


The term “protocol” is also sometimes used when talking about network-based URL schemes. However, not all URL schemes are networking protocols—data:// URLs, for example.

Check this section of the NSURL docs for more information about how to split apart the URL to use the parts you need: Accessing the Parts of the URL (valid link as of 5/21/15)