Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 258
Mostafa Berg Avatar asked Jul 11 '16 07:07

Mostafa Berg


People also ask

What is the latest CocoaPods version?

1.11. 0. beta. 1 - August 09, 2021 (286 KB)


2 Answers

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

enter image description here

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):
like image 133
Edison Avatar answered Oct 26 '22 04:10

Edison


1

let version = Bundle(for: type(of:PODClass)).object(forInfoDictionaryKey:"CFBundleShortVersionString") as? String

2

if let bundle = Bundle.allFrameworks.first(where: { $0.bundleIdentifier?.contains("POD bundleIdentifier or part") ?? false } ) {
    let version = bundle.object(forInfoDictionaryKey:"CFBundleShortVersionString") as? String
}

3

if let url = Bundle.main.resourceURL?.appendingPathComponent("Frameworks/PODName.framework") {
    let version = Bundle(url: url)?.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
like image 28
Alexander Korotkov Avatar answered Oct 26 '22 03:10

Alexander Korotkov