iOS:创建一个链接按钮

时间:2011-07-19 14:06:28

标签: ios xcode mobile-safari

我想为按钮创建一个IBAction,当你按下它时,应用程序会在后台运行,同时在特定链接上打开safari(例如“www.google.it”) 你能救我吗?

4 个答案:

答案 0 :(得分:59)

在IBAction方法中,包括

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

答案 1 :(得分:3)

如果使用界面构建器:

创建UITextView而不是UIButton,将链接作为文本写入textview,然后在界面构建器中选择链接复选框。它将成为您在运行时所需的链接。

答案 2 :(得分:2)

使用swift

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

或swift 3.0

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

答案 3 :(得分: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]];



}