我有一个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.
更多推荐
发布评论