2

I'll try to explain my doubt, but please consider that I'm new to Xcode and I have a lot of things to learn. In my iPhone App I'd like that when I touch an image, the app redirects to an external web URL. Is it possible to do it directly from the Storyboard? I didn't used a ViewController, so I can't call the function.

Thank you in advance.

Undo
  • 25,519
  • 37
  • 106
  • 129
CrazySoftware
  • 133
  • 1
  • 2
  • 10

3 Answers3

2
  1. Add a button to your Storyboard.
  2. Set the button as custom and assign the image as the background of the button.
  3. Launch the Safari browser with your URL from the IBAction of your button.

Here is how to launch the Safari browser with your URL (got this code from here) :

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url])
Community
  • 1
  • 1
Thanakron Tandavas
  • 5,615
  • 5
  • 28
  • 41
1

Easier option is, that I would suggest is use a UIButton with required image, and provide button action method which opens your web URL.

If you want to go with UIImageView only, then add tap gesture recognizer over that, which gives you tap event. Over that event method, you can call your web URL.

Here is a link which provide detail explanation about gesture recognizer.

Hope this will help you.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
0

You can use a UIButton instead of a UIImage and assign the image to the Button in your InterfaceBuilder.

In the IBAction Outlet which must be connected to your UIButton you must then write the following Code:

NSURL *url = [ [ NSURL alloc ] initWithString: @"http://www.cnn.com" ];
[[UIApplication sharedApplication] openURL:url]; 

Since you have to link your storyboard Outlets to a Viewcontroller to handle the UIButton taps, you must use a ViewController.

I also strongly recommend to read about the basics of the MVC Pattern Apple uses in his Apps. I've never seen someone to write an app without using Viewcontrollers. They usually contain all your App Logic.

Maverick1st
  • 3,774
  • 2
  • 34
  • 50