2013-05-31 2 views
0

Как я могу добавить префикс tel: к моему номеру телефона, который я извлек из адресной книги в массиве.добавление префикса tel: к номеру телефона переменная из массива

Если я сделаю это в viewdidload он получает неопределенные

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

Здесь номера телефонов является массив с номерами на каждый контакт

Udate:

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

Опубликовать, что напечатано. Также попробуйте распечатать [NSString stringWithFormat: @ "tel:% @", phoneNumbers] – Durgaprasad

+0

, если вы пытаетесь загрузить его в webView, вам нужно выбрать номера телефонов. Он автоматически обнаружит. – Vignesh

+0

вы хотите добавить префикс «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); 
} 

EDIT: Url был нулевым, потому что были пробелы в "2013-05-31 14: 58: 24.759 GTCallBack [8225: c07]" так кодировать его, чтобы удалить пробелы , Надеюсь, он вам поможет.

+0

Привет Спасибо за ответ, но phoneNumbers показать ОК и phoneUrl все еще null. Я обновлю код с моим вопросом – Anton

+0

похоже на этот 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" ) – Anton

+0

Вы можете распечатать свой массив. Чтобы я мог знать формат вашего массива. –