# How do I launch a URL using Cocoa  (Objective C)



## konan (Jun 5, 2002)

I would like to have my application launch a URL. In windows, the code is like

ShellExecute(...,"http://www.mylink.com",...);

What is the equivalent command using Objective C and Cocoa?

Konan


----------



## Ghoser777 (Jun 6, 2002)

Look in the NSWorkspace docs in the AppKit.  I believe it is in there.

F-bacher


----------



## konan (Jun 6, 2002)

Thanks dude,

Just in case anyone else wants to do this, the code is exactly as follows...

[[NSWorkspace sharedWorkspace] openFile"http://www.mylink.com"];

This one line will open the web site using the default web browser.

Konan


----------



## Joseph Spiros (Jun 13, 2002)

*actually*, you should use the openURL method.. even though it is more code 

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString"http://www.ithinksw.com/"]];


----------



## konan (Jun 13, 2002)

Why is the URL method superior to the document method? Will they not both open the default browser and go to the sight? Is there a performance penality? Please let me know what the difference is so I can make the decision.

The reason why I favor the document method, is that it is more generalized and I can use the same routine to launch a text file, image file or whatever.

If there is a solid reason for using the URL method, I'll make the switch.


Konan


----------



## Joseph Spiros (Jun 13, 2002)

well, the URL method I suggest because.. well, thats what Its made for 

Kind of like how you are "supposed" to use a spoon when eating icecream, but you CAN use a fork.. its just kind of how you are supposed to use it


----------



## konan (Jun 13, 2002)

Well, if you have one of those nifty swiss army knives with a fork, spoon, knife (and maybe a rocket launcher), then that is more handy than a mere spoon 

Konan


----------



## konan (Jul 3, 2002)

Hey Joseph,

It turns out that the openFile method just "stopped working" for me. The only thing I can think of that is different is that my network has changed (perhaps openFile doesn't like my firewall???). In any case, I am using the openURL method instead. 

Thanks for your help 
Konan


----------



## olekeh (Dec 2, 2012)

konan said:


> Why is the URL method superior to the document method? Will they not both open the default browser and go to the sight? Is there a performance penality? Please let me know what the difference is so I can make the decision.
> The reason why I favor the document method, is that it is more generalized and I can use the same routine to launch a text file, image file or whatever.
> If there is a solid reason for using the URL method, I'll make the switch.
> Konan



I tried both. The URL version worked, the other did not.


----------

