Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch safari from a Mac application

Tags:

macos

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.

like image 793
lostInTransit Avatar asked Dec 19 '08 13:12

lostInTransit


1 Answers

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

like image 68
Alnitak Avatar answered Oct 02 '22 23:10

Alnitak