Iphone 如何从UITextField中输入的号码拨打电话?

Iphone 如何从UITextField中输入的号码拨打电话?,iphone,cocoa-touch,Iphone,Cocoa Touch,我想通过用户在文本字段中输入的电话号码进行按键通话。我有一个代码,但它不工作 NSString * phoneNumber = [NSString stringWithFormat:@"%@%@", @"tel://", phoneNumber.text]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 有人对此有类似的看法吗?谢谢。我想是tel:而不是tel://。试一试: NSS

我想通过用户在文本字段中输入的电话号码进行按键通话。我有一个代码,但它不工作

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

有人对此有类似的看法吗?谢谢。

我想是
tel:
而不是
tel://
。试一试:

NSString *pn = [@"tel:" stringByAppendingString:phoneNumber.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:pn]];
有关处理输入无效情况的示例代码,请参阅

基本上你是这样做的:

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

更新:我注意到您创建的字符串共享some name(“phoneNumber”)作为文本字段,您可以从中获取文本。您可能想重命名这两个选项中的任何一个。

问题是,您是否在设备或模拟器上尝试它。@Hadi:Ha,打得好。他可能只是在模拟器上使用它好。。。您是对的,文档只显示“tel:”,而不是“tel://”,但是如果您使用“tel://”,它肯定可以正常工作。