获取 macOS 默认浏览器名称 - LSCopyDefaultApplicationURLForContentType
Get macOS default browser name - LSCopyDefaultApplicationURLForContentType
我正在 Xcode 中开发 macOS 应用程序。我需要做的一件事是打开 URL 的系统默认网络浏览器。我弹出一个警报,为用户提供此选项。该警报应该显示默认网络浏览器的名称。但是我无法找出默认网络浏览器的名称。
我试过下面的代码:
NSLog(@"%@", LSCopyDefaultApplicationURLForContentType(kUTTypeURL, kLSRolesAll, nil));
它只是 returns file:///Applications/Opera.app/
即使我的默认浏览器设置为 Safari。无论我将默认浏览器更改为(Chrome、Safari、Firefox 等...),上述方法只是 returns Opera 浏览器的 URL。
有谁知道如何找出默认浏览器的名称?我知道如何在默认浏览器中打开 URL,这非常简单,但是获取默认浏览器的名称却不是。
我知道这是可能的,因为像 Tweetbot 这样的应用程序有一个选项说 "Open in Safari",它会更改为您的默认浏览器。
丹,谢谢你的时间。
您可以使用 [[NSWorkspace sharedWorkspace] open:url]
在默认浏览器中打开任何 URL,并使用 [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL: url]
获取给定 [=30= 的默认应用程序的 URL ].
要获取应用程序名称,请尝试 [[NSBundle bundleWithURL:appUrl] objectForInfoDictionaryKey:@"CFBundleDisplayName"]
或 [[NSBundle bundleWithURL:appUrl] objectForInfoDictionaryKey:@"CFBundleName"]
(如果第一个为空)。如果两者都失败了,[appUrl deletingPathExtension] lastPathComponent]
可以作为最后的手段。
在此处查看文档:
https://developer.apple.com/documentation/appkit/nsworkspace/1533463-openurl?language=objc
尝试其他 LaunchServices 方法 LSCopyDefaultApplicationURLForURL
并通过 http
方案
CFURLRef httpURL = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("http://"), NULL);
NSLog(@"%@", LSCopyDefaultApplicationURLForURL(httpURL, kLSRolesAll, nil));
在 Swift 上使用 5
var defaultBrowser: String? {
NSWorkspace.shared.urlForApplication(toOpen: URL(string: "http://")!)
.flatMap(Bundle.init(url:))
.flatMap {
[=10=].object(forInfoDictionaryKey: "CFBundleDisplayName") ?? [=10=].object(forInfoDictionaryKey: "CFBundleName")
}.flatMap {
[=10=] as? String
}
}
//given a fileUrl
CFURLRef helperApplicationURL = LSCopyDefaultApplicationURLForURL((__bridge CFURLRef)fileUrl, kLSRolesAll, NULL);
if (helperApplicationURL != NULL) {
NSString *helperApplicationPath = [(__bridge NSURL *)helperApplicationURL path];
NSString *helperApplicationName = [[NSFileManager defaultManager] displayNameAtPath:helperApplicationPath];
CFRelease(helperApplicationURL);
if ([helperApplicationName containsString:@".app"]) {
helperApplicationName = [helperApplicationName stringByDeletingPathExtension];
}
return helperApplicationName;
}
我正在 Xcode 中开发 macOS 应用程序。我需要做的一件事是打开 URL 的系统默认网络浏览器。我弹出一个警报,为用户提供此选项。该警报应该显示默认网络浏览器的名称。但是我无法找出默认网络浏览器的名称。
我试过下面的代码:
NSLog(@"%@", LSCopyDefaultApplicationURLForContentType(kUTTypeURL, kLSRolesAll, nil));
它只是 returns file:///Applications/Opera.app/
即使我的默认浏览器设置为 Safari。无论我将默认浏览器更改为(Chrome、Safari、Firefox 等...),上述方法只是 returns Opera 浏览器的 URL。
有谁知道如何找出默认浏览器的名称?我知道如何在默认浏览器中打开 URL,这非常简单,但是获取默认浏览器的名称却不是。
我知道这是可能的,因为像 Tweetbot 这样的应用程序有一个选项说 "Open in Safari",它会更改为您的默认浏览器。
丹,谢谢你的时间。
您可以使用 [[NSWorkspace sharedWorkspace] open:url]
在默认浏览器中打开任何 URL,并使用 [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL: url]
获取给定 [=30= 的默认应用程序的 URL ].
要获取应用程序名称,请尝试 [[NSBundle bundleWithURL:appUrl] objectForInfoDictionaryKey:@"CFBundleDisplayName"]
或 [[NSBundle bundleWithURL:appUrl] objectForInfoDictionaryKey:@"CFBundleName"]
(如果第一个为空)。如果两者都失败了,[appUrl deletingPathExtension] lastPathComponent]
可以作为最后的手段。
在此处查看文档:
https://developer.apple.com/documentation/appkit/nsworkspace/1533463-openurl?language=objc
尝试其他 LaunchServices 方法 LSCopyDefaultApplicationURLForURL
并通过 http
方案
CFURLRef httpURL = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("http://"), NULL);
NSLog(@"%@", LSCopyDefaultApplicationURLForURL(httpURL, kLSRolesAll, nil));
在 Swift 上使用
var defaultBrowser: String? {
NSWorkspace.shared.urlForApplication(toOpen: URL(string: "http://")!)
.flatMap(Bundle.init(url:))
.flatMap {
[=10=].object(forInfoDictionaryKey: "CFBundleDisplayName") ?? [=10=].object(forInfoDictionaryKey: "CFBundleName")
}.flatMap {
[=10=] as? String
}
}
//given a fileUrl
CFURLRef helperApplicationURL = LSCopyDefaultApplicationURLForURL((__bridge CFURLRef)fileUrl, kLSRolesAll, NULL);
if (helperApplicationURL != NULL) {
NSString *helperApplicationPath = [(__bridge NSURL *)helperApplicationURL path];
NSString *helperApplicationName = [[NSFileManager defaultManager] displayNameAtPath:helperApplicationPath];
CFRelease(helperApplicationURL);
if ([helperApplicationName containsString:@".app"]) {
helperApplicationName = [helperApplicationName stringByDeletingPathExtension];
}
return helperApplicationName;
}