如何从应用程序拨打电话号码

时间:2011-10-25 12:35:25

标签: iphone phone-call

我正在显示名片视图,其中我有一个按钮,我将按钮标题设置为解析后获得的电话号码字符串。我在按钮标题上获得了完美的电话号码字符串值。现在按下该按钮我想调用默认的手机应用程序,以便用户可以打电话。

 -(void) BcardDisp: (id)sender
   {
BGView.hidden = NO;
if(BcardView.hidden == YES)
{

BcardView.hidden = NO;

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    marker *aMarker = (marker *)[appDelegate.markers objectAtIndex:selectedIndexPath.row];
for (int selectedIndexPath = 0; selectedIndexPath < [appDelegate.markers count]; selectedIndexPath++)
{

ShowroomName.text = aMarker.name;
    Address_Bcard.numberOfLines=3;
    Address_Bcard.text =aMarker.address;

    [p_Bcard setTitle:[NSString stringWithFormat:@"%@",aMarker.phone]  forState:UIControlStateNormal];
}
    [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}

}

对于拨打电话号码,我在按钮上使用以下操作。

 - (IBAction)callfrom_BcardVeiw
  {
marker *aMarker = [[marker alloc] init];
NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"%d",aMarker.phone]];

[[UIApplication sharedApplication] openURL:phoneNumberURL];
NSLog(@"%d",phoneNumberURL);

  } 

但是我无法打电话.....获得垃圾价值。我应该在我的 - (IBAction)callfrom_BcardVeiw ....下面放什么逻辑,以便我可以调用与Button标题字符串相同的数字。

3 个答案:

答案 0 :(得分:1)

试试这个:

NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",aMarker.phone]];

答案 1 :(得分:0)

使用“tel://”前缀电话网址。

小心一点,aMarker.phone是整数还是字符串?

在代码的一部分中,您使用“%d”,因此它假定它是一个整数,而在代码的另一部分中,您使用“%@”,假设它是NSObject,可能NSString

我打赌电话号码是一个字符串(因为它可以包含除数字以外的字符,即“+”表示intl前缀,或“*”或“#”表示某些服务...)所以你需要使用{ {1}}而不是@"tel://%@"或者它会格式化您网址中的奇怪值(确切地说是内存中字符串的地址),而不是实际的电话号码。

另一方面,如果电话号码是一个整数(会很奇怪,但你使用“%d”让我感到困惑)并且你使用“%@”,你的代码会崩溃,试图访问描述方法一些不是NSObject的整数值(它将整数视为内存中NSObject的地址,而不是,这将解释BAD_ACCESS)

答案 2 :(得分:0)

删除所有不需要的空间或其他包机:

NSString *phoneNumber = [aMarker.phone stringByReplacingOccurrencesOfString:@" " withString:@""];

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

if ([[UIApplication sharedApplication] canOpenURL:phoneNumberURL ]) {
   [[UIApplication sharedApplication] openURL:phoneNumberURL];
} else {
   // iPad or iPod Touch, can't make a call
}
相关问题