Fresh Beginning
  • Home
  • Portfolio
  • Buy Me A Coffee
  • Patreon
  • Speaking
  • Reading
  • About 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
Portfolio

Portfolio

Hello, I am Jayesh Kawli. I have been writing iOS applications for the last 9 years. During this time, I got a chance to work on iOS applications from various domains. Below is the list of all the apps I was responsible for developing and releasing in the app store.
Jan 30, 2023 6 min read
Using MVVM Architecture with SwiftUI - Clean Architecture

Using MVVM Architecture with SwiftUI - Clean Architecture

MVVM has been a choice of architecture in iOS development for a long time. It survived the Objective-C era and has been quite popular in Swift world too. However, things became slightly complicated when SwiftUI came along. It was a bit confusing to see how it can be used in
Oct 24, 2022 8 min read
A Better Way to Generate Reuse Identifiers in Swift and iOS

A Better Way to Generate Reuse Identifiers in Swift and iOS

iOS offers an elegant way to improve table view and collection view performance by cell reuse. These scroll views may have millions of cells, but iOS only stores the visible cells in memory and reuses them as they go out of view. This enables OS to save on memory and
Oct 20, 2022 2 min read
Fresh Beginning © 2023
Powered by Ghost