2013-04-09 4 views
0

Я новичок в разработке iPhone. У меня есть представление таблицы с 6 разделами, каждая секция имеет одну строку, в разделе 4Th добавляется UILabel. Этот текст UILabel является URL (www.google.com). Я хочу открыть сафари, когда я нажимаю на этом лейбле, но я не успех для открытого сафариВеб-браузер Safari не открывается с URL-адресом

я FOLLO этого UILabel with a hyperlink inside a UITableViewCell should open safari webbrowser?

Но это не работает.

Мой код:

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.backgroundColor = [Prep defaultBGColor]; 

     if(indexPath.section == 3) 
     { 
      self.lblWebsite = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 270, 35)]; 
      self.lblWebsite.backgroundColor = [UIColor clearColor]; 
      self.lblWebsite.text= @"www.gmail.com"; 
      self.lblWebsite.font =[UIFont fontWithName:@"Arial-BoldMT" size:16]; 
      self.lblWebsite.textAlignment = UITextAlignmentLeft; 
      self.lblWebsite.userInteractionEnabled = YES; 
      self.lblWebsite.textColor=[UIColor blackColor]; 
      [cell.contentView addSubview:self.lblWebsite]; 

      UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:)]; 
      gestureRec.numberOfTouchesRequired = 1; 
      gestureRec.numberOfTapsRequired = 1; 
      [self.lblWebsite addGestureRecognizer:gestureRec]; 
      [gestureRec release]; 
     } 
    } 
    return Cell; 
} 

Метод

- (void)openUrl:(id)sender 
{ 

    UIGestureRecognizer *rec = (UIGestureRecognizer *)sender; 

    id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches]; 

    if ([hitLabel isKindOfClass:[UILabel class]]) { 
     NSLog(@"%@",((UILabel *)hitLabel).text); 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]]; 
    } 
} 

Вот что моя ошибка ??

+1

использование http: // также ... –

ответ

2
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]]; 

ур отсутствует "http://"

+0

Thaks for help me :) – 2013-04-09 11:26:18

2

Вы сделали правильно, но, как я думаю, что использование http:// перед тем www.google.com как

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"]; 

if (![[UIApplication sharedApplication] openURL:url]) 

NSLog(@"%@%@",@"Failed to open url:",[url description]); 

и я думаю, что он может работать для вас

0

Just Add " http: // "будет работать Пример: -

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com"]]; 
+0

Что нового в вашем ответе это решение дается до долгого времени. – 2013-04-09 11:11:33

Смежные вопросы