Reputation: 2374
I would like to open an url in a respective default app/browser from my app when a user clicks on a button. The opened web page should get focus.
I have found
[[NSWorkspace sharedWorkspace]
openURL:[NSURL URLWithString:@"https://www.example.com"]];
but it opens the page at the background (at least if Safari is already running). The browser will just "blicks" and keeps to be visible for a few miliseconds and than go into the background.
I have also tried NSApplication sharedApplication
but this did nothing - it did not even opened the web page at all.
What to call so that the Safari will also get focus? Or at least notify the user visually that something happened in Safari.
Or do I need to follow another approach?
Upvotes: 1
Views: 1650
Reputation: 8793
NSWorkspace openURL should just do it all correctly. If it doesn't, I'd look if your app does anything that might undo Safari being frontmost, like calling orderFront on one of your windows.
NSApplication openURL is for the case where you implement a browser of your own (or another app that handles a URL scheme), so it's not surprising that it doesn't do anything.
Other things you can look into if you want to bring an application to the front are NSRunningApplication and Launch services, but I'm a bit surprised that the browser doesn't come to front for you.
Or it could be related to this option in Safari preferences:
If toggling this setting (which is off on a newly-installed Mac) fixes your issue, you may need to use NSRunningApplication to bring Safari to the front explicitly.
Upvotes: 3