4

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!

3 Answers3

5
-(IBAction)clicked:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
}
SSteve
  • 10,550
  • 5
  • 46
  • 72
5

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];
}
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
0

Also look and feel you want to appear button as hyperlink then remove the border of NSButton and use above two answer code for opening the url.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56