在我的目标-c程序中,无论系统的默认浏览器是什么,我都需要在Safari中打开一个URL。这意味着这是行不通的,因为它可以启动Firefox或其他浏览器:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURL: url];
我想我已经接近这个了:
[ws launchAppWithBundleIdentifier: @"com.apple.Safari"
options: NSWorkspaceLaunchWithoutActivation
additionalEventParamDescriptor: NULL
launchIdentifier: nil];
只需要知道如何将URL作为参数传递.有更简单的方法吗?
谢谢!
更新:下面的代码用我想要的URL启动Safari,但是Safari立即终止!知道为什么会这样吗?
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURLs: urls withAppBundleIdentifier:@"com.apple.Safari"
options: NSWorkspaceLaunchDefault
additionalEventParamDescriptor: NULL
launchIdentifiers: NULL];
我在LSOpenFromURLSpec
上观察到了同样的行为。如果Safari实例正在运行,它可以正常工作。如果没有运行Safari实例,它将启动一个新实例并立即终止它。
更新2: Safari只对有Flash嵌入的网站崩溃。使用上面的代码,我可以很好地打开google.com,但是YouTube视频会崩溃。
发布于 2010-06-03 06:47:04
尝试使用来自OpenURLs
的NSWorkspace
方法
- (BOOL) openURLs:(NSArray *)urls
withAppBundleIdentifier:(NSString *)bundleIdentifier
options:(NSWorkspaceLaunchOptions)options
additionalEventParamDescriptor:(NSAppleEventDescriptor *)descriptor
launchIdentifiers:(NSArray **)identifiers
发布于 2011-08-19 19:55:38
使用这个打开默认浏览器中的url .
NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com/"];
if( ![[NSWorkspace sharedWorkspace] openURL:url] )
NSLog(@"Failed to open url: %@",[url description]);
发布于 2010-06-03 22:36:24
我上面列出的两个选项实际上适用于不包括Flash电影的网站。
我描述的崩溃似乎是一个bug,甚至可以用一个Applescript来再现。我对此提出了一个单独的问题(AppleScript to open URL in Safari crashes for Flash-based websites)
为了记录在案,我问题的答案是要么使用LSOpenFromURLSpec
,要么使用下面的代码:
NSWorkspace * ws = [NSWorkspace sharedWorkspace];
[ws openURLs: urls withAppBundleIdentifier:@"com.apple.Safari"
options: NSWorkspaceLaunchDefault
additionalEventParamDescriptor: NULL
launchIdentifiers: NULL];
https://stackoverflow.com/questions/2965615
复制相似问题