How to get build number and version in code

Hey how do I get the current build number and version in code? I need it so that i can attatch it to files in order for the program to know if the file needs to be updated to the current model. It would be preferable if you can show me in swift.

1. Copy this script into a new run script phase under (Select the Blue Project Icon > Build Phases)

2. Click the ➕ button

3. Paste the script below


#!/bin/bash
# Auto Increment Version Script
buildPlist=$INFOPLIST_FILE
echo $buildPlist
CFSVString=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$buildPlist")
CFBundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$buildPlist")
BUILD_NR=${CFBundleVersion##*.}
    BUILD_NR=$(($BUILD_NR + 1))
    #echo $BUILD_NR
    CFBundleVersion=$CFSVString".0."$BUILD_NR
    #echo $CFBundleVersion
    /usr/libexec/PlistBuddy  -c "Set :CFBundleVersion $CFBundleVersion" "$buildPlist"


I add this extension to UIApplication


extension UIApplication {
    struct Constants {
        static let CFBundleShortVersionString = "CFBundleShortVersionString"
    }
    class func appVersion() -> String {
        return NSBundle.mainBundle().objectForInfoDictionaryKey(Constants.CFBundleShortVersionString) as! String
    }
  
    class func appBuild() -> String {
        return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
    }
  
    class func versionBuild() -> String {
        let version = appVersion(), build = appBuild()
      
        return version == build ? "v\(version)" : "v\(version)(\(build))"
    }
}



Then use it on a label like so:


  @IBOutlet weak var appVersionLabel: UILabel! { didSet {
        appVersionLabel.text = "Build Version: \(UIApplication.versionBuild())"
        }
    }

This is for incrementing RN xcode project versions automatically : agvtool next-version -all && xcrun agvtool new-marketing-version \"1.$(agvtool what-version | sed -n 2p |tr -d ' ')\"

or in nodejs

"scripts":{ "ios-next-version":"cd ios && agvtool next-version -all && xcrun agvtool new-marketing-version \"1.$(agvtool what-version | sed -n 2p |tr -d ' ')\" && cd .." }

How to get build number and version in code
 
 
Q