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

I'm currently trying to create a way to register the current version of my Pod at runtime, there are several ways that come to mind, but I wanted to ask here to make sure that I donโ€™t miss something obvious.

What i have done so far:

  • I found out that Cocoapods generates the header file myPod-umbrella.h , which exports the following:

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

    They only myPodVersionNumber seem to be available, and for some reason it always has 1.0, is there a way to make this work correctly, since I have a feeling that this is the right way, but I configured it incorrectly.

  • Try to get hold of Info.plistโ€™s built-in infrastructure and read the version there, but it seems a little problematic, I canโ€™t guarantee that the developer will eventually do the package and may end up with a different bundle location or even have it inaccessible to the project.

  • Create a hardcoded property with the version number, this obviously works, but it adds a lot of room for error and doesn't seem to be the correct way to implement it, but if there is no other way around the CocoaPods, I just have to do it.

  • You have a build step that will read PodSpec and create a simple class that contains metadata about Pod, a little better than the previous one, but still slightly overloads 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 run something like this

print("Current version: \(myPod.version) ")

and run it correctly in the console

#Current version: 1.2.0

+6
source share
1 answer

How about using URLForResource? Prints well at runtime with the print request you requested.

enter image description here

This version displays the entire lock file on 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. Using componentsSeparatedByString("-") , I can remove the character - before the pod name, so that it looks cleaner. This works because lock files use - for each line in the list of subcategory names. Note that we are using pathForResource not a URLForResource .

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

In the next version, we will print two lines. We use the data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) method. In this case, it becomes difficult to remove - so it is 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): 
+2
source

All Articles