tags:

views:

2617

answers:

5

Hi,

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.

A: 

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

Mugunth Kumar
I tried that earlier without success. Thanks.
denton
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
+1  A: 

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
+2  A: 

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
Dashes are OK per the documentation: https://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html
bentford
+5  A: 

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

bentford
A: 

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 scape 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.

samiq