What is plist in iOS?


In this tutorial, we will talk about how we can use the plist in the application to store information. Let’s first understand what is plist.

What is plist?

The plist file is also called the property list and is used by operating systems (e.g. iOS) and users. The information is stored in a key-value format similar to a dictionary.

A plist file can contain different types of values like strings, arrays, dictionaries, booleans, dates, data, and numbers, such as integers. Most of these types correspond to native Swift types, such as Array and Bool. You can also store nested arrays and dictionaries, i.e. adding an array inside another array, etc., to create more complex data structures.

What is info.plist?

The info.plist file contains critical information about your local preferences. It is used to store the configuration of a mobile app. We can store settings information in a plist file.

This file is automatically created when you create a new application. The information saved in this file is used by the App Store and by the operating system to determine the app’s capabilities and to locate key resources. It is required to include this file and it must be named Info.plist otherwise the compiler won't run the application.

What are the predefined values of the info.plist file?

  • Bundle name and identifier

  • Supported interface orientations

  • Launch screen interface file base name

  • Location access permissions

  • Camera access permissions

  • Custom key−values

There are many predefined keys you can use to store information. There are many different types of permissions you can use. For example, if your application is accessing the user's location in the app, then you have to add permission along with the proper message about why you are accessing the user's location in the application so that app users will be able to understand the reason behind this.

The plist format

The plist file's format contains a series of key-value pairs encoded in XML. The keys are always strings, and the values can be strings, numbers, arrays, dictionaries, or dates. When you open a plist file in a text editor, it looks like a jumble of data. However, the data has been organized in a format that is easy for computers to read.

<?xml version=1.0″ encoding=UTF-8?> <!DOCTYPE plist PUBLIC-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”> <plist version=1.0> <dict> <key>Name</key> <string>Amit Mishra</string> <key>Age</key> <integer>40</integer> <key>Children</key> <array> <string>Rahul</string> <string>Aman</string> <string>Nitya</string> </array> </dict> </plist>

In this example, the file contains three key-value pairs. The first pair has a key of “Name” and a value of “Amit Mishra”. The second pair has a key of “Age” and a value of 40 (an integer). The third pair has a key of “Children” and a value of an array of strings. Arrays and dictionaries can contain other key-value pairs and arrays. This allows you to store data in a hierarchical format.

How to read plist values in Swift?

var dictionary: [String: Any] { if let path = Bundle.main.path(forResource: "Info", ofType: "plist"), let dictionary = NSDictionary(contentsOfFile: path) as? [String : Any] { return dictionary } return [:] }

Note − It’s dangerous to manually edit plist files unless you know exactly what you’re doing. The app may crash and not work properly if you make a mistake. Before you make any changes, keep a backup copy of the original plist file that you can revert to in case something goes wrong.

Conclusion

You can also edit an application’s plist file manually, but it can be very tricky depending on how a particular application writes data to a plist file. Knowledge of XML structure helps. For example, if you want to change the font size for the TextEdit application, you would open the com.apple.TextEdit.plist file in a text editor or Xcode. You would find the key related to font size, change the value, save the plist file, and then exit the editing application.

Now you know that property lists are useful for reading and writing user settings to a file, and they’re easy to edit in Xcode.

Updated on: 09-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements