35

I have some code which attempts to make a call within an application, but it doesn't seem to be working:

    UIApplication *myApp = [UIApplication sharedApplication];
    NSString *theCall = [NSString stringWithFormat:@"tel://%@",phone];
    NSLog(@"making call with %@",theCall);
    [myApp openURL:[NSURL URLWithString:theCall]];

Sometimes, the variable phone is something such as @"(102) 222-2222". How can I make a call with a phone number like this? Do I need to manually extract the numbers out of it and get rid of all the extra punctuation?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

3 Answers3

80

Yup. You need to take those out yourself. Or you can use the snippet below...

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", cleanedString]];

Note: you may be tempted to use -stringByTrimmingCharactersInSet:, but that one only removes characters at the start and the end of the string, not if they appear in the middle.

Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • 1
    According to the docs I don't think you need the // in the url. [Apple's phone URL scheme](http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html#//apple_ref/doc/uid/TP40007893-SW1) – marcfrodi Oct 24 '12 at 05:21
  • You are right marcfrodi. Indeed not needed, though iOS won't trip over them either. Anyway, answer has been updated. – Johan Kool Oct 24 '12 at 07:27
  • is there a way to get callbacks if call fail due to low balance or network signal problem – Rajan Maheshwari Feb 24 '15 at 10:43
  • @JohanKool Why not just use -`stringByReplacingOccurencesOfString:withString:` instead for removing the hypens? – Evan R Jul 02 '16 at 23:44
  • @EvanR My snippet doesn't actually remove any hyphens, because that's not what causes the URL to fail to open. It removes any disallowed characters. Surely there is a way to implement similar code using `-stringByReplacingOccurencesOfString:withString:`, but I doubt it'll be as concise as this. – Johan Kool Jul 03 '16 at 03:03
  • @JohanKool Ah, you're right--sorry, didn't look at the OP's question closely enough! – Evan R Jul 03 '16 at 13:51
23

To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

just an update on above answer.

D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
1

Here's a simple method that can be used to make a call and return to the app after the call is finished.

Add the following to your .m file

- (void) dialNumber:(NSString*) number{
number = [@"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}

Then add the following code wherever you want to make the call from:

[self dialNumber:@"5031234567"];
Trianna Brannon
  • 1,246
  • 11
  • 12