【问题标题】:Retrieve pod version from podspec or info.plist in to code从 podspec 或 info.plist 中检索 pod 版本到代码中
【发布时间】:2018-10-15 04:05:56
【问题描述】:

我创建了自己的 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 文件中获取版本。

【问题讨论】:

  • Bundle.main 是应用程序的捆绑包。我建议查看 Bundle documentation 以了解如何为您的 pod 获取捆绑包。应该不会太难。
  • @EmilioPelaez 已尝试但找不到目录“支持文件”

标签: ios swift cocoapods swift4 plist


【解决方案1】:

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

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

例如,我会得到这样的 RxSwift 版本

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

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

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

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

你可以得到这样的结果

"4.3.1"

【讨论】:

    【解决方案2】:

    您可以更明确地检索捆绑包:Bundle(for: SomePodClass.self), 其中SomePodClass 是您的 pod 中的任何类。

    为了方便,您还可以将其转换为 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
    

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多