cyclingIsBetter
cyclingIsBetter

Reputation: 17591

iOS: create a link button

I want to create an IBAction for a button, that when you push it, the app go in background and at the same time open safari at a specific link (example "www.google.it") Can you help me?

Upvotes: 24

Views: 36774

Answers (4)

cristallo
cristallo

Reputation: 2089

using swift

UIApplication.sharedApplication().openURL(NSURL(string: "http://www.google.com")!)

or swift 3.0

UIApplication.shared.openURL(URL(string: "http://google.com")!)

Upvotes: 2

deepa
deepa

Reputation: 11

@interface ViewController : UIViewController<NSURLConnectionDelegate>
@property (strong, nonatomic) IBOutlet UIButton *okbutton;
@property (strong, nonatomic) IBOutlet UIButton *canel;
@property (strong, nonatomic) IBOutlet UITextField *textfiled;
- (IBAction)okButtonClick:(id)sender;

- (IBAction)CanelButton:(id)sender;



- (IBAction)okButtonClick:(id)sender {


    NSString *searchString = textfiled.text; // UItextField.text
    NSString *finalString = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",searchString];

    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:finalString]];



}

Upvotes: 0

Daniel
Daniel

Reputation: 577

If you use interface builder :

Create a UITextView and not a UIButton, write the link as text to textview and select the links checkbox in the interface builder. It will become the link you want at run time.

Upvotes: 3

Patrick Perini
Patrick Perini

Reputation: 22633

Inside your IBAction method, include the line

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

Upvotes: 59

Related Questions