Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make NSButton a Hyperlink in Cocoa

Tags:

cocoa

nsbutton

I was wondering how to make an NSButton a hyperlink so it opens an external webpage in the user's default browser.

Thanks in advance!


2 Answers

-(IBAction)clicked:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
}
like image 100
SSteve Avatar answered Sep 17 '25 19:09

SSteve


You cannot make an NSButton a hyperlink because a hyperlink is an HTML concept while a button is a Cocoa interface component.

But you can use a button's target/action to open a URL programmatically. The action could be a method like:

- (IBAction)openSomeURL:(id)sender
{
    NSURL *myURL = [NSURL URLWithString:@"http://google.com/"];
    [[NSWorkspace sharedWorkspace] openURL:myURL];
}
like image 20
Nikolai Ruhe Avatar answered Sep 17 '25 20:09

Nikolai Ruhe