2013-04-09 3 views
0

iPhone 개발 중입니다. 6 개 섹션이있는 테이블보기가 있습니다. 각 섹션에는 하나의 행이 있습니다. 4Th 섹션에는 UILabel이 추가됩니다. 이 UILabel 텍스트는 URL (www.google.com)입니다. 내가이 레이블을 클릭하면 사파리를 열고 자하지만 난이 UILabel with a hyperlink inside a UITableViewCell should open safari webbrowser?사파리 웹 서블릿은 URL이 열려 있지 않습니다.

을 FOLLO 그러나 그것이 작동하지 열린 사파리

에 대한 성공 아닙니다.

내 코드 :

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

내 실수는 무엇입니까? "http://는"

+1

http : //도 사용하십시오 ... –

답변

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

http : // "작동합니다 예 : -

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

도와주세요. –

2

당신이 바로 수행하지만 난

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

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

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

처럼 www.google.com 전에 사용 http://을 생각하고 내가 "당신이

0

그냥 추가하기 위해이 작동 생각없는 UR

+0

답변에 새로운 내용이 오랫동안 제공됩니다. –

관련 문제