Расширение ответа Франческо Герминары в Swift 4, macOS 10.13.2:
extension Bundle {
class func bundleIDFor(appNamed appName: String) -> String? {
if let appPath = NSWorkspace.shared.fullPath(forApplication: appName) {
if let itsBundle = Bundle(path: appPath) { // < in my build this condition fails if we're looking for the ID of the app we're running...
if let itsID = itsBundle.bundleIdentifier {
return itsID
}
} else {
//Attempt to get the current running app.
//This is probably too simplistic a catch for every single possibility
if let ownID = Bundle.main.bundleIdentifier {
return ownID
}
}
}
return nil
}
}
Поместив его в свой проект Swift, вы можете назвать его так:
let id = Bundle.bundleIDFor(appNamed: "Mail.app")
или
let id = Bundle.bundleIDFor(appNamed: "Mail")