0

I'd like to make a button call a phone number entered by the user inside the text field. I have a code but it doesn't work.

NSString * phoneNumber = [NSString stringWithFormat:@"%@%@", @"tel://", phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

Anyone has a similar approach to this? Thanks.

Johan Kool
  • 15,637
  • 8
  • 64
  • 81
jsanmtosj
  • 563
  • 1
  • 7
  • 12

2 Answers2

2

I think it's tel: instead of tel://. See this Apple document. Try giving this a shot:

NSString *pn = [@"tel:" stringByAppendingString:phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:pn]];
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • Well... you are right that the docs only show "tel:", not "tel://", but it definitely works just fine if you use "tel://". – Johan Kool Aug 10 '11 at 04:31
0

See my answer to another question for some sample code to handle cases with invalid input.

Basically you do this:

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

Update: I noticed that the string you created shares the some name ("phoneNumber") as the text field from which you try to get the text. You may want to rename either of those two.

Community
  • 1
  • 1
Johan Kool
  • 15,637
  • 8
  • 64
  • 81