通过cocoa app在safari中打开网址

Var*_*run 16 safari macos cocoa

我想通过我在Safari中的应用程序在Cocoa中打开一个URL.我在用:

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: @"my url"]];
Run Code Online (Sandbox Code Playgroud)

但问题是如果我的默认浏览器不是Safari,那么URL会在其他浏览器中打开.但我希望我的URL只能在Safari中打开.请告诉解决方案.

谢谢 :)

Ole*_*nko 8

let url = URL(string:"https://twitter.com/intent/tweet")!
NSWorkspace.shared.open([url], 
                        withAppBundleIdentifier:"com.apple.Safari", 
                        options: [],
                        additionalEventParamDescriptor: nil,
                        launchIdentifiers: nil)
Run Code Online (Sandbox Code Playgroud)


kch*_*kch 6

使用NSWorkspaceopenURLs(_:?withAppBundleIdentifier:?options:?additionalEventParamDescriptor:?launchIdentifiers:):

let url = NSURL(string:"http://example.com")!
let browserBundleIdentifier = "com.apple.Safari"

NSWorkspace.sharedWorkspace().openURLs([url],
               withAppBundleIdentifier:browserBundleIdentifier,
                               options:nil,
        additionalEventParamDescriptor:nil,
                     launchIdentifiers:nil)
Run Code Online (Sandbox Code Playgroud)


Neh*_*eha 3

与safari一起使用scripting bridge,在safari中打开一个URL,你会在文件中找到打开url的方法Safari.h。要了解有关使用的更多信息,Scripting bridge请参阅链接以及将脚本桥与 safari 和生成结合使用Safari.h,请参阅我的答案

在 Safari 中打开 URL 的方法是:

NSDictionary *theProperties = [NSDictionary dictionaryWithObject:@"https://www.google.co.in/" forKey:@"URL"];
SafariDocument *doc = [[[sfApp classForScriptingClass:@"document"] alloc] initWithProperties:theProperties];
[[sfApp documents] addObject:doc];
[doc release];
Run Code Online (Sandbox Code Playgroud)