What is the best way to get a specific CocoaPod's version at runtime?

半城伤御伤魂 提交于 2019-12-22 04:15:14

问题


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:

  • 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[];
    

    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.

  • 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.

  • 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.

  • 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("Current version: \(myPod.version)")

and have it log it out properly in the console

#Current version: 1.2.0


回答1:


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

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):

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):


来源:https://stackoverflow.com/questions/38300945/what-is-the-best-way-to-get-a-specific-cocoapods-version-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!