首页 > 解决方案 > 如何以编程方式获取已安装的 mapbox SDK 的当前版本?

问题描述

我在 MapBox.h 中发现了这些行:

/// Project version number for Mapbox.
FOUNDATION_EXPORT MGL_EXPORT double MapboxVersionNumber;

/// Project version string for Mapbox.
FOUNDATION_EXPORT MGL_EXPORT const unsigned char MapboxVersionString[];

这些似乎是全局导出,我可以在任何地方使用MapboxVersionNumber和访问我的代码中的它们MapboxVersionString。但是,它们都是 5 位数字(如果您计算.0双精度数,则为 6 位数字)。我一直在寻找更像的东西4.0.1

标签: iosmapbox

解决方案


您可以通过创建Bundle使用给定标识符初始化的实例来从 Mapbox Info.plist 文件中提取信息

在 Swift 4.2 中你可以试试这个:

extension Bundle {

    var releaseVersionNumber: String? {
        // Actually retrieves the information from the plist.
        return infoDictionary?["CFBundleShortVersionString"] as? String
    }

    var buildVersionNumber: String? {
        // Actually retrieves the information from the plist.
        return infoDictionary?["CFBundleVersion"] as? String
    }

    static var mapbox: Bundle {
        // Crash can occur if you are not linking Mapbox and "com.mapbox.sdk.ios" does not exist.
        return Bundle.init(identifier: "com.mapbox.sdk.ios")!
    }
}

然后你可以在项目的任何地方使用它:

let mapboxVersion = Bundle.mapbox.releaseVersionNumber
print("Mapbox version: \(mapboxVersion)")

注意:如果 Mapbox 以某种方式决定更改其标识符字符串,或者如果您想对另一个框架做同样的事情,您可以打印所有现有的嵌入框架:

for frameworkBundle in Bundle.allFrameworks {
    print("Framework bundle identifier: \(frameworkBundle.bundleIdentifier ?? "No identifier")")
}

推荐阅读