我有一個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]];
}
}