2010-03-08 90 views
0

我有一个tableView,其中包含带电话号码的单元格。该应用程序没有拨号。请参见下面的代码为什么我的“tel:”链接不能正常工作

- (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]]; 
    } 
} 

回答

5

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

  • 空间
  • 散列(#
  • 星号(*

iPhone Dev Center

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

URLs for URLs for Telephone Calls RFC

...空间不能在电话中使用 号码网址作为空格字符 不能在URL中使用而不 逃脱它。

1

在电话号码中不能有空格。剥离并重试。

2

您需要使用例如NSStringstringByAddingPercentEscapesUsingEncoding:方法转义空格。

0

st3fan是对的,您应该在构造NSURL实例之前使用stringByAddingPercentEscapesUsingEncoding。 iPhone OS为你自动执行其他魔法。如果你不会百分之百地逃避问题字符,那么URLWithString:方法将返回nil。