【问题标题】:How do I access the Info.plist values from Swift in Xcode 12如何在 Xcode 12 中从 Swift 访问 Info.plist 值
【发布时间】:2021-10-18 22:19:31
【问题描述】:

我希望能够从我的 iOS 应用程序中访问 Info.plist 中的版本控制值,在 Xcode 12.5 中使用 Swift 编写。有问题的键/值是:

CFBundleShortVersionString: $(MARKETING_VERSION)
CFBundleVersion: $(CURRENT_PROJECT_VERSION)

在 Build Settings 中,两个 Info.plist 值分别包含“1.0”和“8”。我尝试了以下方法:

let appBundleVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String

如果我在 Info.plist 中有用户定义的键/值对,则该方法有效,但不适用于访问系统管理的构建设置。如何访问这两个值?

谢谢。

【问题讨论】:

    标签: ios swift xcode build


    【解决方案1】:

    我的问题中暗示了答案。我在 Info.plist 文件中定义了两个用户定义的键/值对:

    App_version_number: $(MARKETING_VERSION)
    App_build_number: $(CURRENT_PROJECT_VERSION)
    

    并在代码中引用了这些:

    let appBuildNumber = Bundle.main.object(forInfoDictionaryKey: "App_build_number") as? String
    let appVersionNumber = Bundle.main.object(forInfoDictionaryKey: "App_version_number") as? String
    

    也许有一种直接的方法可以访问构建设置,但这种间接方法效果很好。

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多