Reputation: 563
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.
Upvotes: 31
Views: 41935
Reputation: 1061
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.
Upvotes: 1
Reputation: 12194
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.
Upvotes: 1
Reputation: 2974
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.
Upvotes: 7
Reputation: 33406
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
Upvotes: 53
Reputation: 14298
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?
Upvotes: 1
Reputation: 2119
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:@""];
Upvotes: 3
Reputation: 2339
(
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)
Upvotes: 7
Reputation: 50697
If your phone number has (
or )
in it, the Phone app will not launch.
Upvotes: 5
Reputation: 13433
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.
Upvotes: 3
Reputation: 6360
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.
Upvotes: 2
Reputation: 14499
The URL should be tel://3035551212 and not tel:3035551212... Add that // and it should work.
Upvotes: -4