con*_*fin 0 iphone cocoa-touch
我有一个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]];
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
2010-03-08 01:32:30.830 AIB[1217:207] tel:01 8350098
如您所见,该号码将转到控制台,但不会拨打.奇怪的是,如果我将最后一个语句更改为:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:171"]];
Run Code Online (Sandbox Code Playgroud)
手机拨打号码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]];
}
}
Run Code Online (Sandbox Code Playgroud)
您需要清理用户输入,使其成为有效的tel://URL.具体来说,这包括剥离:
#)*)来自iPhone开发中心:
为防止用户恶意重定向电话或更改电话或帐户的行为,Phone应用程序支持tel方案中的大多数但不是全部特殊字符.具体来说,如果URL包含*或#字符,则电话应用程序不会尝试拨打相应的电话号码.
来自电话呼叫URL的URL:
...空格绝不能用在URL中的电话号码中,因为空格字符不能在URL中使用而不能转义它.