Fresh Beginning
  • Home
  • Portfolio
  • My Clothing Shop
  • Etsy Clothing Store
  • Buy Me A Coffee
  • Patreon
  • Speaking
  • Reading
  • About Me
  • Contact Me
Subscribe
iOS

How to get the iOS App Metadata in Swift

  • Jayesh Kawli

Jayesh Kawli

Nov 3, 2018 • 2 min read

Working on a new app could be exciting. However, there are certain things or information which you might want to get as a part of the core feature. One of them is the app metadata. This may contain numerous values - Some of them essential some - not so useful.

Some examples of metadata info could be,

  1. Version number
  2. Build number
  3. Language
  4. Region
  5. Open source libraries
  6. Credits
  7. Environment
  8. Terms and conditions
  9. The build stability

However, not all the apps might be using all of these or they can even add their own mix to it.

Today I am going to talk about only some of the fields in metadata. They are listed as follow,

  1. Version number
  2. Build number
  3. Application name
  4. App environment

1. Getting environment flag

Let's see how we can start gathering this information. First off, to check whether it's Debug build or not, we will have to add a special flag to Xcode build settings. This flag will allow us to check the environment.

  1. Click on the project (On left side)
  2. Go to tab under Build Settings
  3. Scroll to the section titled Other Swift Flags
  4. Now double click the Debug subsection and add -DDEBUG as a flag in addition to pre-existing flags in that section

Screen-Shot-2018-11-03-at-2.27.38-PM

Now we're all set as long as build settings are concerned. Now let's go back to the code

2. Getting version, build, and app name info

In order to receive this info, we will have to access the infoDictionary object associated with the main bundle. Once retrieved, we can then read required information using knowns keys,

if let infoDictionary = Bundle.main.infoDictionary {
    let version = infoDictionary["CFBundleShortVersionString"] as? String
    let build = infoDictionary[kCFBundleVersionKey as String] as? String
    let appName = infoDictionary[kCFBundleNameKey as String] as? String
}

As you can see in the above example, we have to use as String for some keys. This is because infoDictionary only accepts keys which are of type String. However, in this case, both kCFBundleVersionKey and kCFBundleNameKey were of type CFString which is why we have to explicitly cast them to String.

Once this is done, let's move on to calculating our app environment. Since you've already added debug flag in the Build Settings, it is as easy as just a few lines,

var appEnvironment: String?
#if DEBUG
    appEnvironment = "Debug"
#else
    appEnvironment = "Release"
#endif

Now since we have all the information, let's group this into a nice little function and we're all set,

3. Full code

func appMetadata() -> String? {
    var shortAppMetadata: String?
    if let infoDictionary = Bundle.main.infoDictionary {
        let version = infoDictionary["CFBundleShortVersionString"] as? String
        let build = infoDictionary[kCFBundleVersionKey as String] as? String
        let appName = infoDictionary[kCFBundleNameKey as String] as? String
        var appEnvironment: String?
        #if DEBUG
            appEnvironment = "Debug"
        #else
            appEnvironment = "Release"
        #endif

        if let version = version, let build = build, let appName = appName, let appEnvironment = appEnvironment {
            shortAppMetadata = "App Metadata -> \(appName) Version: \(version) Build: \(build) in \(appEnvironment)"
        } else {
            assert(false, "Unable to get app metadata at least one of the sub-parts of the information missing")
        }
    }
    return shortAppMetadata
}

4. Output

Which, when executed, outputs the following string:

"App Metadata -> SampleSwiftCode Version: 1.0 Build: 1 in Debug"
Please check out my Patreon and BuyMeACoffee pages too.
If you like my articles and want to keep me going on this journey, please consider donating on these platforms.
Your support in any form is much appreciated.
Buy me a coffee support

Patreon support

Sign up for more like this.

Enter your email
Subscribe
Announcing my Clothing Shop - Burnousstraat Designs

Announcing my Clothing Shop - Burnousstraat Designs

Life is too short to wear boring clothes - Anonymous Hey folks, this is going to be a slightly different post than usual. I've got some exciting news to share with you all – we've officially launched Burnousstraat Designs, your new go-to destination for one-of-a-kind and unique graphic apparel! At Burnousstraat
Mar 20, 2024 2 min read
Using RxSwift to Populate Data on UITableView (iOS and Swift)

Using RxSwift to Populate Data on UITableView (iOS and Swift)

It's been a while since I posted. But better late than never. In today's blog post, I am going to write about how to use RxSwift to populate data in UITableView on iOS. RxSwift is a library for composing asynchronous and event-based code by using observable sequences and functional style
Oct 26, 2023 6 min read
SwiftUI: Apply Custom Formatting to Localized Strings

SwiftUI: Apply Custom Formatting to Localized Strings

In today's post, I am going to touch on a niche area in SwiftUI designs and string formatting. This is about how to apply custom formatting to localizable strings. It sounds complicated, so let me explain it with an example, Suppose I have a localizable string like this, Going from
Sep 27, 2023 6 min read
Fresh Beginning © 2025
Powered by Ghost