无论系统浏览器设置为什么,都可以使用 Safari 打开 URL [英] Open URL with Safari no matter what system browser is set to

查看:186
本文介绍了无论系统浏览器设置为什么,都可以使用 Safari 打开 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的objective-c 程序中,无论系统的默认浏览器是什么,我都需要在Safari 中打开一个URL.这意味着这将不起作用,因为它可以启动 Firefox 或任何其他浏览器:

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];

我想我很接近这个:

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

只需要弄清楚如何将URL作为参数传入...有没有更简单的方法?

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

谢谢!

更新: 以下代码使用我想要的 URL 启动 Safari,但 Safari 立即终止!知道这是为什么吗?

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];

我观察到与 LSOpenFromURLSpec 相同的行为.如果 Safari 实例正在运行,则它可以正常工作.如果没有 Safari 实例正在运行,它会启动一个新实例并立即终止它.

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.

更新 2: Safari 只会在嵌入 Flash 的网站上崩溃.使用上面的代码,我可以很好地打开 google.com,但是 Safari 浏览 YouTube 视频时会崩溃,例如.

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.

推荐答案

尝试 NSWorkspace:

Try the OpenURLs method from NSWorkspace:

- (BOOL) openURLs:(NSArray *)urls
         withAppBundleIdentifier:(NSString *)bundleIdentifier
         options:(NSWorkspaceLaunchOptions)options
         additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor
         launchIdentifiers:(NSArray **)identifiers

这篇关于无论系统浏览器设置为什么,都可以使用 Safari 打开 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆