检测您的cocoa swift应用程序正在运行的位置

时间:2015-01-17 00:16:28

标签: cocoa swift

我正在尝试但无法确定是否有办法使用swift来检测正在运行的.app文件的路径和名称

即如果我的应用程序是selfupdater.app并且它存储在~username / applications

如何在swift

中将selfupdater.app作为字符串变量启动时获取此信息

1 个答案:

答案 0 :(得分:0)

这称为主捆绑文件夹。

Swift 3或更高版本

extension URL {
    var parentDirectory: URL? {
        return (try? resourceValues(forKeys: [.parentDirectoryURLKey]))?.parentDirectory
    }
}

let bundleURL = Bundle.main.bundleURL

if let path = bundleURL.parentDirectory?.path {
    print(path)
}