tags:

views:

336

answers:

2

Hi

How can I launch the Safari browser or the user's default browser pointing it to a specific address from within my Mac application?

I am using Objective-C as the programming language.

Thanks.

+3  A: 

From a shell you can use the open command with a URL as a parameter, and that takes care of opening that URL in the default browser.

So you should be able to use system() or similar fork()/exec() code to do the same.

nb: open will also open other sorts of files / URLs, too, so make sure it really is a web URL you're trying to open otherwise you've got a probable security problem.

The Objective C way of doing it appears to be:

[[NSWorkspace sharedWorkspace] openURL:url];

where url is a pointer to an NSURL object

Alnitak
A: 

Here is exactly how to do it in full (as of 2010):

Create a function in YourView.m

-(IBAction)buyLinkPressed:(id)sender
    {
    [[NSWorkspace sharedWorkspace] openURL:[NSURL
        URLWithString:
        @"http://itunes.apple.com/us/app/gamename/id1234?mt=8"]];
    }

of course, include

-(IBAction)buyLinkPressed:(id`enter code here`)sender;

in YourView.h file.

Open YourView.xib. Add a new button on your view, type "momentary push in". Click that round blue arrow icon at the top of the Inspector panel. In "Sent Action", "Selector", click and hold down in the small circle. Drag al the way to "File's Owner" (blue cube) in the other window, and then on to "buyLinkPressed:". You're done! Hope it helps.

Joe Blow