当按钮按下时如何打开通话/拨号数字视图?

时间:2011-07-09 09:07:23

标签: ios objective-c uibutton phone-call

enter code here-(IBAction)Call:(id)sender{
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://18005551234"]];
/*NSString *phoneStr = [[NSString alloc] initWithFormat:@"tel:%@",phoneNumber.text];
NSURL *phoneURL = [[NSURL alloc] initWithString:phoneStr];
[[UIApplication sharedApplication] openURL:phoneURL];
[phoneURL release];
[phoneStr release];*/
NSURL *url = [ [ NSURL alloc ] initWithString: @"tel:212-555-1234" ];
[[UIApplication sharedApplication] openURL:url]; 

} 在上面的代码我使用各种方法,但没有人工作。当我在函数上实现断点时,它会说这超出了范围。这有什么问题?

1 个答案:

答案 0 :(得分:2)

您应该从电话号码中删除连字符“ - ”,以及括号“(”,“)”。除了数字之外,不应该有任何特殊字符。

NSCharacterSet *specialCharSet = [NSCharacterSet characterSetWithCharactersInString:@" )(-,"];
NSArray *components = [phoneNumber.text componentsSeparatedByCharactersInSet:specialCharSet];
NSString *phoneStr = [components componentsJoinedByString:@""];
phoneStr = [NSString stringWithFormat:@"tel:%@", phoneStr];
NSURL *url = [[NSURL alloc] initWithString:phoneStr];
[[UIApplication sharedApplication] openURL:url];