从 podspec 或 info.plist 中检索 pod 版本到代码中

Retrieve pod version from podspec or info.plist in to code

我创建了自己的 pod,它有包含 s.version = "0.4.7" 的 podspec 文件,我想以编程方式将其写入代码,因此每当应用程序运行时,它都会将 pod 版本发送到服务器。

另一个获取 pod 版本的地方是从下面的 plist 文件,它是“Bundle version string”0.4.7

我试过使用 let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String 但它从 Apps Info.plist 文件中获取版本。

您可以通过特定的库包获取 pod 的版本。

首先,点击 Pods Project 文件,它将显示您在 Podfile 中安装的所有库。

比如我会这样获取RxSwift的版本

  • 第 1 步:通过单击 RxSwift Target > General > Copy 获取 RxSwift 的 Bundle Identifier Bundle Identifier (org.cocoapods.RxSwift)

  • 第二步:通过此代码获取版本

(注意 org.cocoapods.RxSwift 是您从第 1 步得到的)

if let version = Bundle(identifier: "org.cocoapods.RxSwift")?.infoDictionary?["CFBundleShortVersionString"] as? String {
    print(version)
}

你可以得到这样的结果

"4.3.1"

您可以更明确地检索包:Bundle(for: SomePodClass.self), 其中 SomePodClass 是您的广告连播中的任何 class。

为了方便,您也可以将其转换为 Pod 内的扩展:

public extension Bundle {
    class var somePodName: Bundle {
        return Bundle(for: SomePodClass.self)
    }
}

为您的 Pod 编写一些配置文件:

public enum SomePodConfiguration {
    static var version: String {
        guard let version = Bundle.somePodName
            .infoDictionary?["CFBundleShortVersionString"] as? String else { return "Unknown" }
        return version
    }
}

最后得到 Pod 版本:

SomePodConfiguration.somePodName.version