9

How to read App Name, Version, and Build Number from Info.plist

 3 years ago
source link: https://sarunw.com/posts/how-to-read-info-plist/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to read App Name, Version, and Build Number from Info.plist

17 May 2021 ⋅ 4 min read ⋅ Swift Development

Table of Contents

Info.plist is a place to store information about your app, e.g., App name, app version, and build number. You might want to use this information somewhere like About view or attached them in troubleshooting.

I have shown one way to read a property list in How to read a Property List (plist) into the code, but for Info.plist, Apple provided a more convenient method for us.

Read any plist file

The following is an example of how to retrieving values from the Info.plist using the method mentioned in How to read a Property List (plist) into the code.

var config: [String: Any]?

if let infoPlistPath = Bundle.main.url(forResource: "Info", withExtension: "plist") {
do {
let infoPlistData = try Data(contentsOf: infoPlistPath)

if let dict = try PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: Any] {
config = dict
}
} catch {
print(error)
}
}

print(config?["CFBundleName"])
// Optional(example-info)

print(config?["CFBundleVersion"])
// Optional(1)

print(config?["CFBundleShortVersionString"])
// Optional(1.0)

We specified a path to the plist file and parsed it to a dictionary using PropertyListSerialization.

sponsor.jpg

Read value from Info.plist

Since each bundle must contain an Info.plist file describing the bundle and the format of the plist is predefine and known by the system. Apple does us a favor and group up the file locating and parsing logic into one simple method, object(forInfoDictionaryKey:).

object(forInfoDictionaryKey:) is a Bundle's instance method which returns the value associated with the specified key in the receiver's information property list (Info.plist).

Using this method can save you a lot of boilerplate code you see in the Read any plist file method.

print(Bundle.main.object(forInfoDictionaryKey: "CFBundleName"))
// Optional(example-info)

print(Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion"))
// Optional(1.0)

print(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString"))
// Optional(1)

We can retrieve the value associated with the key in Info.plist directly using object(forInfoDictionaryKey:), which is quite convenient and suit this simple tasks like reading application information.

Where can I find the key

You can visit Apple Documentation - Information Property List Key Reference (which might contain too much information than you need) or you can find it directly in your Info.plist file.

To see a raw key for your Info.plist entry:

  1. Open your Info.plist file.

  2. Right-click anywhere on the editor screen. The context menu will open up, select Raw Keys & Values.
    infoplist-read-change-raw.png

  3. Keys will then show in the raw format, which you can use as an argument for object(forInfoDictionaryKey:).
    infoplist-read-raw.png

Caveat

There is one behavior different between using object(forInfoDictionaryKey:) and PropertyListSerialization, that is the value returned from object(forInfoDictionaryKey:) will be localized if available.

To demonstrate this, I have added a new key, CustomKey, and assign a value to Hello.

Add a new key namedAdd a new key named "CustomKey" with a value of "Hello".

Then I create a localized string for Info.plist with Thai translation of สวัสดี.

Add translation for newly created key.Add translation for newly created key.

Then we try to retrieve this value using both methods.

Read any plist file

var config: [String: Any]?

if let infoPlistPath = Bundle.main.url(forResource: "Info", withExtension: "plist") {
do {
let infoPlistData = try Data(contentsOf: infoPlistPath)

if let dict = try PropertyListSerialization.propertyList(from: infoPlistData, options: [], format: nil) as? [String: Any] {
config = dict
}
} catch {
print(error)
}
}

print(config?["CustomKey"])
// Optional(Hello)

Reading the plist file won't take localization into account.

Read value from Info.plist

print(Bundle.main.object(forInfoDictionaryKey: "CustomKey"))
// Optional(สวัสดี)

Using object(forInfoDictionaryKey:) will use the localized value if available.

You might want to use one method over another based on your need.

Extension

You can write an extension to Bundle to make it more readable like this.

extension Bundle {
var appName: String? {
return object(forInfoDictionaryKey: "CFBundleName") as? String
}

var appVersion: String? {
return object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}

var buildNumber: String? {
return object(forInfoDictionaryKey: "CFBundleVersion") as? String
}
}

It is easier to use and more meaningful this way.

print(Bundle.main.appName)
print(Bundle.main.appVersion)
print(Bundle.main.buildNumber)

You may also like

How to read a Property List (plist) into the code

When I want to keep some information or configuration out of a code base and don't want something fancy, I usually save it as Property List file. Let's see how to do it.

Swift
Tuist init: How to use Tuist templates to bootstrap your project

Learn how to use, and limitations of tuist init, a command that bootstrap a new project.

Development
SwiftUI's ViewModifier

Learn a crucial concept in SwiftUI, view modifier, and a guide of how to create your custom modifier.

Swift

Read more article about Swift, Development,

or see all available topic

Get new posts weekly

If you enjoy this article, you can subscribe to the weekly newsletter.

Every Friday, you’ll get a quick recap of all articles and tips posted on this site — entirely for free.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron

Tweet

Share

Previous
How to set up iOS environments: develop, staging, and production

Learn how to create a separate environment for your app with the help of Configuration and Scheme. Create a different app and variables for each environment on the same codebase.

Next
How to make AppBar/Navigation Bar transparent in Flutter

Learn how to make a transparent app bar in Flutter.

← Home


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK