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?
Inside your IBAction method, include the line
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
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.
using swift
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.google.com")!)
or swift 3.0
UIApplication.shared.openURL(URL(string: "http://google.com")!)
@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]];
}
精彩评论