Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open URL with Safari no matter what system browser is set to

In my objective-c program, I need to open a URL in Safari no matter what the system's default browser is. That means that this won't work, because it could launch Firefox or whatever other browser:

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURL: url];

I think I'm close with this:

[ws launchAppWithBundleIdentifier: @"com.apple.Safari"
                          options: NSWorkspaceLaunchWithoutActivation
   additionalEventParamDescriptor: NULL
                 launchIdentifier: nil];

only need to figure out how to pass in the URL as parameter... Is there an easier way?

Thanks!

Update: The following code launches Safari with the URL I want, but Safari terminates right away! Any ideas why this is?

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURLs: urls withAppBundleIdentifier:@"com.apple.Safari" 
     options: NSWorkspaceLaunchDefault
additionalEventParamDescriptor: NULL
launchIdentifiers: NULL];

I observed the same behavior with LSOpenFromURLSpec. If a Safari instance is running, it works fine. If no Safari instance was running, it starts a new one and terminates it right away.

Update 2: Safari only crashes for web sites that have Flash embedded. With the code above, I can open google.com just fine, however Safari crashes for a YouTube video, for example.

like image 479
Mark Avatar asked Jun 03 '10 11:06

Mark


People also ask

How do I set Safari to open links as default?

On your Mac, choose Apple menu > System Preferences, then click General. Click the “Default web browser” pop-up menu and choose Safari.

How do I force Safari to open a website?

Choose View > Reload Page. If the page still won't open, quit Safari, reopen it, then try again. Try again at a different time. The website server may be busy, or the website may be unavailable temporarily.

What is the problem when Safari not opening a Perticular website?

The first thing you should do is check your internet connection, make sure it is active and enabled. This applies to Mac, iPhone, iPad, and anything really. If the device or computer is disconnected from the internet, you will see the “Safari Can't Open the Page” every time.

How do I stop unwanted websites opening automatically Safari?

On your iPhone, iPad, or iPod touch, go to Settings > Safari and turn on Block Pop-ups and Fraudulent Website Warning. On your Mac, you can find these options in Safari > Preferences. The Websites tab includes options to block some or all pop-up windows, and you can turn on fraudulent site warnings in the Security tab.


3 Answers

Try the OpenURLs method from NSWorkspace:

- (BOOL) openURLs:(NSArray *)urls
         withAppBundleIdentifier:(NSString *)bundleIdentifier
         options:(NSWorkspaceLaunchOptions)options
         additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor
         launchIdentifiers:(NSArray **)identifiers
like image 97
Jon Shier Avatar answered Oct 14 '22 11:10

Jon Shier


Use this to open the url in the default browser...

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com/"];
if( ![[NSWorkspace sharedWorkspace] openURL:url] )
    NSLog(@"Failed to open url: %@",[url description]);
like image 33
Roger Sodré Avatar answered Oct 14 '22 10:10

Roger Sodré


The two options I listed above actually work for websites that don't include Flash movies.

The crash I described seems to be a bug that can even be reproduced with a single Applescript. I opened a separate question for this (AppleScript to open URL in Safari crashes for Flash-based websites)

For the record, the answer to my question is to either use LSOpenFromURLSpec or this code:

NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURLs: urls withAppBundleIdentifier:@"com.apple.Safari" 
  options: NSWorkspaceLaunchDefault
  additionalEventParamDescriptor: NULL
  launchIdentifiers: NULL];
like image 2
Mark Avatar answered Oct 14 '22 11:10

Mark