0

I am new at iPhone Development. I have Table view with 6 section each section has one row, in 4Th Section i add UILabel. This UILabel text is URL (www.google.com). I want to open safari when i click on This label but i am not success for open safari

i follo this UILabel with a hyperlink inside a UITableViewCell should open safari webbrowser?

But it not work.

My code:

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

Method

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

Here what is my mistake ??

Community
  • 1
  • 1

3 Answers3

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

u r missing "http://"

yunas
  • 4,143
  • 1
  • 32
  • 38
2

You done right but as i think use http:// before www.google.com like

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

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

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

and i think it may work for you

Buntylm
  • 7,345
  • 1
  • 31
  • 51
0

Just Add "http://" it will work Example:-

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com"]];
ios_av
  • 324
  • 3
  • 8
  • what's new in your answer this solution are given before a long time. –  Apr 09 '13 at 11:11