Saturday, June 4, 2011

International Dialing OUT codes

A while ago, I have posted a tutorial of how to launch your own application via a custom URL (link). While working with the option to provide calling functionality in our application, many developers come across the problem of handling international dialing codes.

As an example, consider the case where user has a number and wants to dial those number via the app. So far so good .. The problem is that say the number is in the UK then the number is prefixed with +44
So if user is in Tunisia and tapped the phone number the code would be:

00 44

If user is in the US it would be

011 44

Handling such international dialing isn't a big problem. The solution lies here:
Make sure you remove any brackets from phone number and prefix it with +. That's it

NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:+%@, thePhoneNumber]];

[[UIApplication sharedApplication] openUrl:phoneNumberURL];


Remember it will only work if you have the dial code for the country in question already appended with the phone number e.g. 44 for the UK.

And if you don't know (or don't care to find a way) to remove brackets and blank spaces from the phone number, here is the method for it.
NSMutableString *phone = [[phone_number mutableCopy] autorelease];
[phone replaceOccurrencesOfString:@" "
withString:@""
options:NSLiteralSearch
range:NSMakeRange(0, [phone length])];
[phone replaceOccurrencesOfString:@"("
withString:@""
options:NSLiteralSearch
range:NSMakeRange(0, [phone length])];
[phone replaceOccurrencesOfString:@")"
withString:@""
options:NSLiteralSearch
range:NSMakeRange(0, [phone length])];