30

I'm trying to initiate a call from within an iPhone app.

This related code works and opens Safari as expected:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];

But, when I replace the http URL with a tel URL the resulting code does not invoke the phone app:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:3035551212"]];

No exceptions or alerts are generated (in the simulator or on a device).

Any idea what the problem might be with my invocation?

Thanks.

denton
  • 553
  • 1
  • 6
  • 8
  • 1
    it should work, maybe there is something prior in the code. what is the device ? an ipod touch can't phone ... – CiNN May 01 '09 at 17:10

11 Answers11

52

The iphone will dial a number using either of the formats listed below. But, it will do nothing if you are in the simulator. It took me 30 minutes of banging my head to figure this out.

[[UIApplication sharedApplication] 
                    openURL:[NSURL URLWithString:@"tel://15415551234"]];

[[UIApplication sharedApplication] 
                    openURL:[NSURL URLWithString:@"tel:15415551234"]];

[[UIApplication sharedApplication] 
                    openURL:[NSURL URLWithString:@"tel:1-541-555-1234"]];

Link for Apple documentation on the tel: url scheme

Link for openURL documentation

Yasmin Tiomkin
  • 282
  • 1
  • 5
  • 14
bentford
  • 33,038
  • 7
  • 61
  • 57
7

As @bentford said one might get miscarried because the simulator does show an alert when you try to click on a phone on the contacts app, this is just an alert that gets generated because the app checks whether or not the tel: protocol is supported on the device or not.

Adding to what he writes you might want to also add support to escape any special characters or spaces as in:

NSString *phoneStr = [NSString stringWithFormat:@"tel:%@",[self.contactDetails objectForKey:@"phone"]];
NSString *escaped = [phoneStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];

Hope it helps.

Cheers.

Stunner
  • 12,025
  • 12
  • 86
  • 145
samiq
  • 2,954
  • 1
  • 29
  • 29
7

(and )are actually no problem if you use stringByAddingPercentEscapesUsingEncoding as suggested by samiq. Same goes for +, / and spaces. It's a URL, so escaping seems natural.

If I'm guessing right, Apple's regular phone number recognition will be used in the tel: scheme handler, if you escape correctly.

(missing reputation to make it a comment)

Sebastian
  • 2,109
  • 1
  • 20
  • 15
4

If your phone number has ( or ) in it, the Phone app will not launch.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • FWIW, I've tried several alternatives including `(xxx)-xxx-xxxx` and parenthesis doesn't seem to be a problem on iOS 7. Though *blank spaces* seems to hinder the Phone app from launching. – Gabriel Osorio Jul 30 '14 at 19:22
3

I found that the issue was due to characters that where not allowed for tel:// URI - the following code solved my problem:

NSMutableCharacterSet *charSet = [NSMutableCharacterSet new];
[charSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
NSArray *arrayWithNumbers = [str componentsSeparatedByCharactersInSet:charSet];
NSString *numberStr = [arrayWithNumbers componentsJoinedByString:@""];
Gil Margolin
  • 1,959
  • 20
  • 21
3

Haven't had any problems with it using tel:{phone-number} and invoking it the same way you are. Only works on the iPhone device, though.

One thing I did have to do was strip out extraneous characters (like all parentheses, spaces, dashes and dots) out of the phone string. Some of the examples above (but not the original post) have dashes. That might be the problem.

Ramin
  • 13,343
  • 3
  • 33
  • 35
  • Dashes are OK per the documentation: https://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html – bentford Oct 27 '09 at 05:24
2

I just ran into this when trying to add a "Call" button to a UIAlertView. I had the following code to handle the call:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != 0)
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:1-602-555-1212"]];
    }
}

It wouldn't open anything, just like you. I even tried a regular http URL. It turned out I had forgotten to set the delegate to self. That's probably your problem also.

bbrown
  • 6,370
  • 5
  • 37
  • 43
1

I got the same problem and at last I found the reason was that there was a space in the phone number (for better formatting). After removing the space it worked fine.

Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30
boreas
  • 1,041
  • 1
  • 15
  • 30
1

Here is an update on how to derive a phone number in 2018 (where iOS 11 and 12 are most common):

NSString *phoneNumberURLString = [@"tel://" stringByAppendingString:[result.phoneNumber
                 stringByAddingPercentEncodingWithAllowedCharacters:
                                 [NSCharacterSet URLPathAllowedCharacterSet]]];
NSURL *phoneNumberURL = [NSURL URLWithString:phoneNumberURLString];

samiq's answer was what I used initially until I found that my app would crash due to a nil NSURL being created (and later being passed into a collection object) due to failing to properly escape a phone number formatted like so: (###) ###-####. The aforementioned code resolves this.

Stunner
  • 12,025
  • 12
  • 86
  • 145
1

some additional info about making phone calls via url scheme (as I think someone may find it useful)

How to use tel: with * (star, asterisk) or # (hash, pound) on iOs?

Community
  • 1
  • 1
Hlung
  • 13,850
  • 6
  • 71
  • 90
-4

The URL should be tel://3035551212 and not tel:3035551212... Add that // and it should work.

Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • 1
    Not true. Either works and Apple's documentation suggests the poster's version: https://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html – bbrown May 13 '09 at 02:22
  • `tel:` URLs don't use "//", and the current version of docs [https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html] do not have examples with `tel://` – benc Oct 11 '19 at 08:09