24

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?

4 Answers 4

59

Inside your IBAction method, include the line

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

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.

2

using swift

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

or swift 3.0

UIApplication.shared.openURL(URL(string: "http://google.com")!)
0
@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]];



}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.