Iphone 为什么我的;电话:";链接不起作用

Iphone 为什么我的;电话:";链接不起作用,iphone,cocoa-touch,Iphone,Cocoa Touch,我有一个tableView,里面有带电话号码的手机。不过,该应用程序并没有拨号。请参见下面的代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath

我有一个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.830aib[1217:207]电话:018350098

如您所见,该号码会转到控制台,但不会被拨打。奇怪的是,如果我把最后一句话改成:

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

电话号码中不能有空格。请将其去掉,然后重试。

电话号码中不能有空格。去除该选项并重试。

您需要使用例如
NSString的
stringByAddingPercentEscapesUsingEncoding:
方法来转义空格。

您需要使用例如
NSString的
stringByAddingPercentEscapesUsingEncoding:
方法来转义空格。

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

  • 空间
  • 散列(
    #
  • 星号(
    *
发件人:

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

发件人:

…电话中不得使用空格 URL中的数字作为空格字符 不能在没有URL的URL中使用 逃离它


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

  • 空间
  • 散列(
    #
  • 星号(
    *
发件人:

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

发件人:

…电话中不得使用空格 URL中的数字作为空格字符 不能在没有URL的URL中使用 逃离它


St3fan是正确的,在构建
NSURL
实例之前,应该使用
stringbyaddingpercentescapesusingencode
。iPhone操作系统会自动为您带来其他魔力。如果您不想对问题字符进行百分比转义,则
URLWithString:
方法将返回nil。

St3fan是正确的,您应该在构建
NSURL
实例之前使用
stringByAddingPercentEscapesUsingEncoding
。iPhone操作系统会自动为您带来其他魔力。如果不转义问题字符,则
URLWithString:
方法将返回nil