为什么我的“tel:”链接不起作用(Why are my “tel:” links not working)

编程入门 行业动态 更新时间:2024-10-26 13:20:57
为什么我的“tel:”链接不起作用(Why are my “tel:” links not working)

我有一个tableView,其中包含带有电话号码的单元格。 该应用程序不是拨打号码。 请参阅下面的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; NSString *numberToDial = [NSString stringWithFormat:@"tel:%@", selectedCell.detailTextLabel.text]; NSLog(@"%@",numberToDial); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberToDial]]; } }

控制台输出: 2010-03-08 01:32:30.830 AIB[1217:207] tel:01 8350098

如您所见,该数字将转到控制台,但不会拨打。 奇怪的是,如果我将最后一个语句更改为:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:171"]];

手机拨打号码171没有任何问题


如下所述,解决我的特定问题的方法是从电话号码中删除空格。 我实现了如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; NSMutableString *numberToDial = [NSMutableString stringWithFormat:@"tel:%@", selectedCell.detailTextLabel.text]; [numberToDial replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [numberToDial length])]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberToDial]]; } }

I have a tableView which has cells with phone numbers. The app is not dialing the numbers though. See the code below

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; NSString *numberToDial = [NSString stringWithFormat:@"tel:%@", selectedCell.detailTextLabel.text]; NSLog(@"%@",numberToDial); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberToDial]]; } }

Console ouput: 2010-03-08 01:32:30.830 AIB[1217:207] tel:01 8350098

As you can see, the number goes to the console, but doesn't get dialled. The weird thing is, if I change the last statement to this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:171"]];

the phone dials the number 171 without any issue


The solution to my particular problem is, as suggested below, to remove the spaces from the phone numbers. I achieved this as follows:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; NSMutableString *numberToDial = [NSMutableString stringWithFormat:@"tel:%@", selectedCell.detailTextLabel.text]; [numberToDial replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [numberToDial length])]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberToDial]]; } }

最满意答案

您需要清理用户输入,使其成为有效的tel:// URL。 具体来说,这包括剥离:

空间 哈希( # ) 星号( * )

来自iPhone开发中心 :

为防止用户恶意重定向电话或更改电话或帐户的行为,电话应用程序支持tel方案中的大多数但不是全部特殊字符。 具体来说,如果URL包含*或#字符,则电话应用程序不会尝试拨打相应的电话号码。

来自电话呼叫URL的URL :

...空格绝不能用在URL中的电话号码中,因为空格字符不能在URL中使用而不能转义它。

You will need to sanitize the user input for it to be a valid tel:// URL. Specifically this includes stripping of:

Spaces Hashes (#) Asterisks(*)

From iPhone Dev Center:

To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number.

From URLs for URLs for Telephone Calls RFC:

...spaces MUST NOT be used in phone numbers in URLs as the space character cannot be used in URLs without escaping it.

更多推荐

本文发布于:2023-08-06 06:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   链接   tel   links   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!