在运行时获取特定CocoaPod版本的最佳方法是什么? [英] What is the best way to get a specific CocoaPod's version at runtime?

查看:99
本文介绍了在运行时获取特定CocoaPod版本的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一种在运行时记录当前版本的Pod的方法,但我想在这里提出一些方法以确保我没有遗漏明显的东西。

I'm currently trying to create a way to log the current version of my Pod at runtime, there are a few ways that come to mind, but I wanted to ask here to make sure i'm not missing something obvious.

我到目前为止所做的事情:

What I've done so far:


  • 发现了Cocoapods生成一个 myPod-umbrella.h 头文件,该文件导出以下内容:

  • Found out that Cocoapods generates an myPod-umbrella.h header file that exports the following :

FOUNDATION_EXPORT double myPodVersionNumber;
FOUNDATION_EXPORT const unsigned char myPodVersionString[];

他们似乎只能访问 myPodVersionNumber ,并且由于某种原因它始终为1.0,是否有办法使该工作正常,因为我感觉这是正确的方法,但是我配置不正确。

They Only myPodVersionNumber seems to be accessible, and it always has 1.0 for some reason, is there a way to get that working right since i have a feeling that this is the proper way but i have misconfigured it.

试图获取已构建框架的Info.plist并在那里读取版本,但这似乎有点问题,我无法保证开发人员最终会使用pod进行操作,并且最终可能会

Attempt to get a hold of the built framework's Info.plist and read the version there, but this seems to be a bit problematic, i have no guarantee what a developer will end up doing with the pod and might end up with a different location of the bundle, or even have it unaccessible to the project.

使用版本号创建硬编码属性,这显然可行,但它增加了很多有很多错误的余地,并且确实感觉不到实现此问题的正确方法,但是如果没有其他方法可以解决CocoaPods,我可能只需要这样做。

Create a hardcoded property with the version number, this obviously works, but it adds lots of room to error and does not really feel like the right way to implement this, but if there is no other way to get around CocoaPods, i might just have to do that.

具有一个Build步骤,该步骤将读取PodSpec并生成一个包含有关Pod的元数据的简单类,感觉有点比上一点更好,但是对于我正在寻找的东西仍然感到有点过大。

Have a Build step that will read the PodSpec and generate a simple class that contains metadata about the Pod, feels a bit better than the previous point, but still feels a bit overkill for what i'm looking for.

有人吗?更好的主意还是可以向我指出正确的方向?

Does anyone have a better idea or can point me in the right direction ?

我要实现的目标是能够运行这样的东西

What i'm trying to achieve is to be able to run something like this

print(当前版本:\(myPod.version)

并使其在控制台中正确注销

and have it log it out properly in the console

#当前版本:1.2.0

推荐答案

如何使用URLForResource?

What about using URLForResource? Prints nicely at runtime with the print statement you asked for.

此版本将整个锁定文件打印到控制台。

This version prints the entire lock file to the console.

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSBundle.mainBundle().URLForResource("/lockfilefolder/Podfile", withExtension: "lock")!
    let data = try! String(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    print(data)   
}

/* Prints */
// PODS:
// - Firebase/Analytics (3.3.0):
// - FirebaseAnalytics (= 3.2.1)
// - Firebase/Auth (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - FirebaseAuth (= 3.0.3)
// - Firebase/Core (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - Firebase/Database (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - FirebaseDatabase (= 3.0.2)
// TL;DR

下一个版本将打印特定的行号。通过使用 componentsSeparatedByString(-),我可以删除Pod名称之前的-字符,这样看起来更干净。之所以可行,是因为锁定文件在Pod名称列表中的每一行上使用-。请注意,我们此处使用的是 pathForResource ,而不是 URLForResource 。 / p>

This next version prints specific line numbers. By using componentsSeparatedByString("-") I am able to remove the - character before the pod name so it looks cleaner. This works because lock files use - on every line in the list of pod names. Notice we are using pathForResource not URLForResource here.

    do {
        if let path = NSBundle.mainBundle().pathForResource("/lockfilefolder/Podfile", ofType: "lock"){
            let data = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
            let lockFileData = data.componentsSeparatedByString("-")
            print("Current version: \(lockFileData[6])")
        }
    } catch let err as NSError {
        print(err)
    }

/* Prints */
// Current version: - Firebase/Core (3.3.0):

此下一个版本我们打印两行。我们正在使用 data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())方法。在这种情况下,删除-变得很冗长,因此不值得。

This next version we print two lines. We are using the data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) method. It gets verbose to remove the - in this case therefore it's not worth it.

let url = NSBundle.mainBundle().URLForResource("/lockfilefolder/Podfile", withExtension: "lock")!
    let data = try! String(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    let lockFileData = data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
    print(lockFileData[72])
    print(lockFileData[6])

/* Prints */
// COCOAPODS: 0.39.0
// - Firebase/Core (3.3.0):

这篇关于在运行时获取特定CocoaPod版本的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆