如何从应用程序呼叫电话号码

发布于 2024-12-11 23:17:51 字数 1241 浏览 1 评论 0原文

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

 -(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 .... 下放置什么逻辑,以便我可以调用与按钮标题字符串相同的号码。

I am displaying Business card view on which I have one button on which I am setting Button title as phone number string which I got after parsing. I am getting perfect phone number string value on the button title. Now by pressing that button I want to call the default phone app so that User can 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];
}

}

For Calling number I am using following action on Button.

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

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

  } 

But I am not able to call.....getting grabage value. What logic should I put under my - (IBAction)callfrom_BcardVeiw ....so that I can call on the same number as Button title string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰沐子 2024-12-18 23:17:51

试试这个:

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

Try this:

NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",aMarker.phone]];
垂暮老矣 2024-12-18 23:17:51

在电话 URL 前面加上“tel://”前缀。

也要小心,aMarker.phone 是整数还是字符串?

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

我敢打赌电话号码是一个字符串(因为它可以包含数字以外的字符,即“+”表示国际前缀,或“*”或“#”表示某些服务......),因此您需要使用 @ “tel://%@” 而不是 @"tel://%d",否则它会在 URL 中格式化奇怪的值(内存中字符串的地址,即精确)而不是实际的电话号码。

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

Prefix the phone URL with "tel://".

Be careful too, Is aMarker.phone an integer or a string?

In one part of your code you use "%d" so it assumes it is an integer, and in the other part of your code you use "%@", which assumes it is a NSObject, probably an NSString.

I bet the phone number is a string (as it can contains characters others than digits, namely "+" for intl prefixes, or "*" or "#" for some services...) so you need to use @"tel://%@" and not @"tel://%d" or it will format strange values in your URL (the address of the string in memory, to be precise) instead of the actual phone number.

On the other hand, if the phone number is an integer (would be strange but your use of "%d" makes me perplex) and you use "%@", your code will crash, trying to access description method on some integer value that is not an NSObject (it will consider the integer as an address of an NSObject in memory whereas it is not, which will explain the BAD_ACCESS)

晨光如昨 2024-12-18 23:17:51

删除任何空格或其他不需要的章程:

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
}

Remove any space, or other charters that are not needed:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文