Открыть URL-адрес в сафари через приложение cocoa
Я хочу открыть URL-адрес в Cocoa через мое приложение только в Safari. Я использую:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: @"my url"]];
Но проблема в том, что если мой браузер по умолчанию не является Safari, тогда URL-адрес открывается в другом браузере. Но я хочу, чтобы мой URL-адрес открывался только в Safari. Скажите, пожалуйста, решение.
Спасибо:)
Ответы
Ответ 1
Используйте scripting bridge
с сафари, чтобы открыть URL-адрес в сафари, вы найдете способ открыть url в файле Safari.h
.
Чтобы узнать больше об использовании scripting bridge
, обратитесь и используйте скриптовый мост с сафари и сгенерируйте Safari.h
, напишите my ответ здесь.
Метод открытия URL-адреса в Safari:
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
Используйте NSWorkspace 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
Вы не можете использовать URL-адрес, вам нужен NSString
if(![[NSWorkspace sharedWorkspace] openFile:fullPath
withApplication:@"Safari.app"])
[self postStatusMessage:@"unable to open file"];
Ответ 4
Чтобы открыть URL-адрес с любым приложением, вы можете использовать службы запуска.
Функция, которую вы хотите посмотреть, - LSOpenURLsWithRole
;
EDIT:
Вам нужно будет связать инфраструктуру SystemConfiguration с вашим проектом, чтобы этот метод был доступен.
Ссылка на документ Apple здесь
Например, если вы хотите открыть 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
нерестится процесс и выполняет open -a "Safari" http://someurl.foo также делает трюк
Ответ 6
let url = URL(string:"https://twitter.com/intent/tweet")!
NSWorkspace.shared.open([url],
withAppBundleIdentifier:"com.apple.Safari",
options: [],
additionalEventParamDescriptor: nil,
launchIdentifiers: nil)