在 iOS 应用程序中拨打电话

发布于 2024-11-15 02:25:05 字数 399 浏览 3 评论 0原文

我有一些代码尝试在应用程序中拨打电话,但它似乎不起作用:

    UIApplication *myApp = [UIApplication sharedApplication];
    NSString *theCall = [NSString stringWithFormat:@"tel://%@",phone];
    NSLog(@"making call with %@",theCall);
    [myApp openURL:[NSURL URLWithString:theCall]];

有时,变量 phone 类似于 @"(102) 222-第2222章如何使用这样的电话号码拨打电话?我是否需要手动从中提取数字并删除所有多余的标点符号?

I have some code which attempts to make a call within an application, but it doesn't seem to be working:

    UIApplication *myApp = [UIApplication sharedApplication];
    NSString *theCall = [NSString stringWithFormat:@"tel://%@",phone];
    NSLog(@"making call with %@",theCall);
    [myApp openURL:[NSURL URLWithString:theCall]];

Sometimes, the variable phone is something such as @"(102) 222-2222". How can I make a call with a phone number like this? Do I need to manually extract the numbers out of it and get rid of all the extra punctuation?

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

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

发布评论

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

评论(3

滴情不沾 2024-11-22 02:25:06

是的。你需要自己把它们拿出来。或者您可以使用下面的代码片段...

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

注意:您可能会想使用 -stringByTrimmingCharactersInSet:,但是该方法仅删除字符串开头和结尾的字符,而不是如果它们出现在中间。

Yup. You need to take those out yourself. Or you can use the snippet below...

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

Note: you may be tempted to use -stringByTrimmingCharactersInSet:, but that one only removes characters at the start and the end of the string, not if they appear in the middle.

婴鹅 2024-11-22 02:25:06

要返回原始应用程序,您可以使用 telprompt:// 而不是 tel:// - 告诉提示将首先提示用户,但当通话完成后,它将返回您的应用程序:

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

只是对上述答案的更新。

To go back to original app you can use telprompt:// instead of tel:// - The tell prompt will prompt the user first, but when the call is finished it will go back to your app:

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

just an update on above answer.

暮年慕年 2024-11-22 02:25:06

这是一个简单的方法,可用于拨打电话并在通话结束后返回到应用程序。

将以下内容添加到您的 .m 文件中

- (void) dialNumber:(NSString*) number{
number = [@"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}

然后在您想要进行调用的任意位置添加以下代码:

[self dialNumber:@"5031234567"];

Here's a simple method that can be used to make a call and return to the app after the call is finished.

Add the following to your .m file

- (void) dialNumber:(NSString*) number{
number = [@"telprompt://" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}

Then add the following code wherever you want to make the call from:

[self dialNumber:@"5031234567"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文