2014-02-13 8 views
11

Ich möchte eine URL in Cocoa nur über meine App in Safari öffnen. Ich verwende:Öffnen Sie URL in Safari durch Kakao App

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

Aber das Problem ist, dass, wenn mein Standard-Browser nicht Safari ist, dann wird die URL in einem anderen Browser geöffnet wird. Aber ich möchte, dass meine URL nur in Safari geöffnet wird. Bitte sag die Lösung.

Thanks :)

Antwort

3

Verwenden scripting bridge mit Safari eine URL in Safari zu öffnen, werden Sie eine Methode finden url in der öffnen Datei Safari.h. Um mehr über die Verwendung von Scripting bridge zu erfahren, beziehen Sie sich auf die link und Skript-Brücke mit Safari zu verwenden und Safari.h generieren, beziehen Sie sich hier auf meine answer.

Verfahren eine URL in Safari zu öffnen ist:

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

Seine angehängte Datei: /// beim Öffnen der URL, daher öffnet sich die URL nicht. – Varun

+0

Überprüfen Sie die aktualisierte Antwort, es sollte jetzt funktionieren. – Neha

+0

Ja !! Es ist toll! – Genevios

1

Sie nicht URL verwenden können, benötigen Sie einen NSString

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

eine URL mit jeder Anwendung zu öffnen, können Sie die Startdienste nutzen. Die Funktion, die Sie betrachten möchten, ist LSOpenURLsWithRole;

EDIT:
Sie haben die Systemconfiguration Rahmen zu einem Projekt für diese Methode verknüpfen verfügbar sein.

Apple-doc Referenz here

Zum Beispiel, wenn Sie http://www.google.com mit Safari öffnen möchten:

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

einen Prozess Laichen und offen -a Ausführung " Safari "http://someurl.foo macht auch den Trick

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