Question

I have a UIView , and inside that view i add a UIImageView with an image inside of it , using this method :

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageAdName]];    
UIImageView *imgView = [[UIImageView alloc]initWithImage:[[UIImage alloc] initWithData:data]];
imgView.frame = CGRectMake(0, 0, 320, 50);
[_ad1 addSubview:imgView];

I want, when the user touches this view, to be redirected through safari to an external website which i indicate the url. How can i do that?
Many Thanks!

Was it helpful?

Solution

It seems that you would only need to add a gesture recognizer to the UIImageView which you intend the user to tap:

UIImageView *imgView;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                   action:@selector(imageViewTapped:)];
[imgView addGestureRecognizer:tapRecognizer];


- (void)imageViewTapped:(UIGestureRecognizer *)recognizer {
    [[UIApplication sharedApplication] openURL:yourURL];
}

If the URL to be loaded in Mobile Safari depends on which particular image view was tapped, then you have more work to do. Possibly you could subclass UIImageView so that it has a property for the URL it's associated with. (Although that doesn't really sound like a proper role for view.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top