How to open a URL in Safari (on Mac in Cocoa)

Similar to UIApplication, we can use NSApplication to open a URL on Mac. This will launch Safari automatically:

NSURL *yourURL = [NSURL URLWithString:@""];
[[NSWorkspace sharedWorkspace]openURL:yourURL];

Apparently this method returns a BOOL to indicate success of the operation. Note that this is does not indicate success at all, because as long as Safari can be launched, this will return YES – no matter if the URL can be displayed or not.

Test for internet connectivity before use (for example with Tony Million’s Reachability).

Leave a Reply