9

I'm building an app that lets you call different service centers from Netherlands. The problem is that some of them have a different commercial format (like 0800-xxxx) and the device can't make the call. The code looks like this:

if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
    [[UIApplication sharedApplication] openURL:phoneURL];
}

Do you have any idea how to format the number or to make the phone call, no matter it's format?

EDIT: This is how the phoneNumber is created:

NSString *phoneNumberString = phoneNumber; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", phoneNumberString];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
Andra Todorescu
  • 628
  • 10
  • 27

2 Answers2

27

I used this code and it worked:

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];

if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
    [[UIApplication sharedApplication] openURL:phoneURL];
}
Andra Todorescu
  • 628
  • 10
  • 27
1
NSString *strm = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)mob, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);
NSString *strMob = [[NSString alloc] initWithFormat:@"tel://%@",strm];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strMob]];

try this code . This works perfectly for any kind of format.It will convert into perfect calling URL .

  • In this "mob" is for your mobile num which is in NSString format. –  Sep 06 '12 at 10:20
  • I used this code and it worked: NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""]; NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber]; NSURL *phoneURL = [NSURL URLWithString:phoneURLString]; [[UIApplication sharedApplication] openURL:phoneURL]; – Andra Todorescu Sep 06 '12 at 10:24
  • @AndraTodorescu, if you answer your own question, please place the answer in the answer section. Not in a comment. – Black Frog Sep 06 '12 at 10:29