2014-02-13 4 views
11

Safari에서만 내 앱을 통해 코코아에서 URL을 열려고합니다. 나는 다음을 사용하고있다 :코코아 앱을 통한 사파리에서 URL 열기

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

그러나 내 기본 브라우저가 Safari가 아니면 다른 브라우저에서 URL이 열리는 문제가있다. 하지만 내 URL이 Safari에서만 열리길 원합니다. 해결책을 말해주십시오.

감사

답변

3

사파리에서 URL을 엽니 다 사파리와 사용 scripting bridge 필요 :), 당신은에 URL을 열 수있는 방법을 찾을 것입니다 파일 Safari.h. Scripting bridge을 사용하는 방법에 대해 자세히 알고 싶으면 link을 참조하고 사파리가있는 스크립팅 브리지를 사용하여 Safari.h을 생성하십시오. 내 answer을 참조하십시오.

사파리에서 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]; 
+0

대기중인 파일 : ///은 URL을 여는 동안 열려 있지 않으므로 url이 아닙니다. – Varun

+0

업데이트 된 답변을 확인하십시오, 지금 작동합니다. – Neha

+0

예! 그것은 굉장! – Genevios

1

당신은 URL을 사용할 수 없습니다, 당신은있는 NSString

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

는 모든 응용 프로그램에 URL을 열려면 당신은 발사 서비스를 사용할 수 있습니다. 보고 싶은 기능은 LSOpenURLsWithRole입니다.

편집 :
당신은 사용할 수 있습니다이 방법에 대한 귀하의 프로젝트에에서 SystemConfiguration 프레임 워크를 연결해야합니다.

애플 문서 참조 here 예를 들어

당신이 사파리와 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); 
} 
0

과정을 산란하고 개방 -a를 실행 " 사파리 "http://someurl.foo도 트릭을 수행합니다

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