Open url in safari through cocoa app

2019-03-24 16:41发布

问题:

I want to open a URL in Cocoa through my app in Safari only. I am using:

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

But the problem is that if my default browser is not Safari, then the URL gets open in some other browser. But I want my URL to open in Safari only. Please tell the solution.

Thanks :)

回答1:

Use scripting bridge with safari to open a URL in safari, You will find a method to open url in the file Safari.h. To know more about using Scripting bridge refer the link and to use scripting bridge with safari and generate Safari.h, refer my answer here.

The method to open a URL in Safari is:

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


回答2:

Use 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)


回答3:

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


回答4:

You can't use URL, you need a NSString

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


回答5:

To open a URL with any application, you can use the launch services. The function you want to look at is LSOpenURLsWithRole ;

EDIT:
You will have to link the SystemConfiguration framework to your project for this method to be available.

Apple doc reference here

For example if you want to open http://www.google.com with safari :

//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);
}


回答6:

spawning a process and executing open -a "Safari" http://someurl.foo also does the trick