【问题标题】:adding prefix tel: to phone number variable from array将前缀 tel: 添加到数组中的电话号码变量
【发布时间】:2013-05-27 03:49:05
【问题描述】:

如何将前缀 tel:65 添加到我从数组中的地址簿中获取的电话号码中。

如果我在 viewdidload 中执行此操作,它将为空

NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", phoneNumbers]];
    NSLog(@"Some Text %@", phoneUrl);
    NSLog(@"Phone Numbers %@", phoneNumbers);

这里的 phoneNumbers 是每个联系人的号码数组

日期:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"BG.jpg"]]];
    [detailTableView setBackgroundColor:[UIColor clearColor]];
    detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    ShadowTable *Shadow = [[ShadowTable alloc] init];
    [Shadow ForTableView:detailTableView ForView:self.view HeaderAlpha:0.3 FooterAlpha:0.6];

    for(int i = 0 ; i <[phoneNumbers count]; i++)
    {
        NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", [phoneNumbers objectAtIndex:i]]];
        NSLog(@"Phone with URL %@", phoneUrl);
        NSLog(@"Phone Numbers %@", phoneNumbers);
    }


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [phoneTypes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];
    typeLabel.text = [phoneTypes objectAtIndex:indexPath.row];
    [typeLabel setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:typeLabel];



    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 10, 170, 40)];
    numberLabel.text = [phoneNumbers objectAtIndex:indexPath.row];
    [numberLabel setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:numberLabel];

    if (indexPath.row %2 == 0) {

        cell.contentView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
    } else {

        cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    }

    //cell.textLabel.text = [phoneNumbers objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

【问题讨论】:

  • 发布打印的内容。也尝试打印 [NSString stringWithFormat:@"tel:%@", phoneNumbers]
  • 如果您尝试在 webView 中加载它,您必须选择检测电话号码。它会自动检测。
  • 您想为数组中的每个元素添加“tel:”前缀吗?
  • 如果 phonenumbers 是一个数组,你就没有制作正确的字符串。您可以迭代数组以添加电话。

标签: ios objective-c tel


【解决方案1】:

您使用的是数组而不是字符串。因此,您需要访问数组的所有元素以添加为后缀。

使用这个:

for(int i = 0 ; i <[phoneNumbers count]; i++)
{
    NSString *str = [@"tel:" stringByAppendingFormat:@"%@",[phoneNumbers objectAtIndex:i]];
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneUrl = [NSURL URLWithString:str];
NSLog(@"Some Text %@", phoneUrl);
}

编辑: Url 为空,因为“2013-05-31 14:58:24.759 GTCallBack[8225:c07]”中有空格,因此对其进行编码以删除空格。 希望对你有帮助。

【讨论】:

  • 您好,感谢您的回答,但 phoneNumbers 显示正常,phoneUrl 仍然为空。我会用代码更新我的问题
  • 看起来像这样 2013-05-31 14:58:24.758 GTCallBack[8225:c07] 带有 URL 的电话 (null) 2013-05-31 14:58:24.759 GTCallBack[8225:c07]电话号码(“(058)758 0000”)
  • 你能打印你的数组吗?这样我就可以知道你的数组的格式。
  • 已打印:2013-05-31 14:58:24.759 GTCallBack[8225:c07] 此联系人中的电话号码(“(058) 758 0000”)只有一部电话。
  • 我已经更新了我的答案。 Url 为空,因为“2013-05-31 14:58:24.759 GTCallBack[8225:c07]”中有空格,因此对其进行编码。它会工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
相关资源
最近更新 更多