ios - 如何以编程方式获取已安装的 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
解决方案
您可以通过创建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")")
}
推荐阅读
- javascript - 简化一次性消息通道机器人
- mysql - 如何将查询从 sql 转换为 codeigniter
- excel - 创建 Range 表达式以在 Union() 方法中使用
- vtk - 为什么 VTK 会为气球小部件示例代码给出错误“无法找到 vtkTextRenderer 对象”?
- python - OpenCV 捕获多个 RTSP 流 - Python
- facebook - 本机脚本 facebook//google sso 服务器端
- php - 从动态图片 url 获取图片扩展
- html - 为什么刷新浏览器时无法显示pdf文件?
- android - 使用 MainActivity.java 文件和 Window.java 文件创建一个窗口 HelloWorld 应用程序
- jwt - 基于令牌的身份验证:将路由限制为特定用户