通过cocoa app在safari中打开网址

时间:2014-02-13 04:56:25

标签: macos cocoa safari

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

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: @"my url"]];

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

谢谢:)

6 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:5)

使用NSWorkspace's openURLs(_:​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)

答案 2 :(得分:2)

使用safari中的scripting bridge在Safari中打开网址,您会找到一种在文件Safari.h中打开网址的方法。 要了解有关使用Scripting bridge的更多信息,请参阅link并使用带有safari的脚本桥并生成Safari.h,请在此处参阅我的answer

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

答案 3 :(得分:0)

你不能使用URL,你需要一个NSString

if(![[NSWorkspace sharedWorkspace] openFile:fullPath
                            withApplication:@"Safari.app"])
    [self postStatusMessage:@"unable to open file"];

答案 4 :(得分:-1)

要使用任何应用程序打开URL,您可以使用启动服务。 您要查看的功能是LSOpenURLsWithRole;

修改
您必须将SystemConfiguration框架链接到项目才能使此方法可用。

Apple doc reference here

例如,如果您想使用safari打开http://www.google.com

//the url
CFURLRef url = (__bridge CFURLRef)[NSURL URLWithString:@"http://www.google.com"];
//the application
NSString *fileString =  @"/Applications/Safari.app/";

//create an FSRef of the application
FSRef appFSURL;
OSStatus stat2=FSPathMakeRef((const UInt8 *)[fileString UTF8String], &appFSURL, NULL);
if (stat2<0) {
    NSLog(@"Something wrong: %d",stat2);
}


//create the application parameters structure
LSApplicationParameters appParam;
appParam.version = 0;       //should always be zero
appParam.flags = kLSLaunchDefaults; //use the default launch options
appParam.application = &appFSURL; //pass in the reference of applications FSRef

//More info on params below can be found in Launch Services reference
appParam.argv = NULL;
appParam.environment = NULL;
appParam.asyncLaunchRefCon = NULL; 
appParam.initialEvent = NULL;

//array of urls to be opened - in this case a single object array
CFArrayRef array = (__bridge CFArrayRef)[NSArray arrayWithObject:(__bridge id)url];

//open the url with the application
OSStatus stat = LSOpenURLsWithRole(array, kLSRolesAll, NULL, &appParam, NULL, 0);
//kLSRolesAll - the role with which the applicaiton is to be opened (kLSRolesAll accepts any)

if (stat<0) {
    NSLog(@"Something wrong: %d",stat);
}

答案 5 :(得分:-1)

产生一个过程并执行打开-a&#34; Safari&#34; http://someurl.foo 也可以解决问题