Skill Level Beginner
- [Tutor] To keep persistent settings around, you need property lists, often called plists, you as a developer can store some small amounts of data, usually settings in a property list and read it in your code, when you need it. In IOS 11, the codable protocol made plists even easier, if you've seen the earlier tips video on reading and writing JSON, this will look familiar. Download the PropertyList and open it, I added some photo and color assets for you and set up the storyboard to have a photo on it.
We'll set the photo and color of the background using the plist in Assets, you'll see the already created info.plist, that contains many settings for the project, we'll make our own by going to File, New, File. In the IOS templates, scroll down to the Resource section, you'll find a Property List, select that and hit Next, name the Property List Backgrounds, make sure you're in the correct Group and Create the file, you'll get a blank Property List.
To add an item, point your mouse to where it says Root, you'll see a plus appear, click it for a new entry, change the name of the new entry to image and then on the Value side of this, click it and put in cake. Now that was the default String type, but there are other types, you'll see there's a plus after image and you can hit that to make a new entry and the second one I'm gonna call color. Now where it says String, you'll see some arrows, you can click that and see all the different types you can use here, I'm gonna use a number and I'm gonna set this number to one, this is one way you can look at a plist, the other way you can look at it is if you right-click and go to Open As, you'll see where it says Source Code, click that and you get the same thing, this time in XML.
Notice here, the cake is a string and the color is an integer, we're gonna use that in just a second for our next step. Head over to the ViewController, to use the codable protocol, you need to set up a struct that adopts codable, so up above where I have the backgroundImage here, I'm gonna start a struct, I'm gonna call it backgroundSettings and it's got adopt Codable, now inside here, I'm gonna put the two variables in my struct, that are the same things as my plist, they have to have the exact same type to work right, it's one of the reasons I looked at that type, var image, they also have the same name, so I'm gonna make these exactly the same and that's a String and var color, which is an Int.
Now head down to viewDidLoad, let me give myself a little bit more space here, so you can see that on top, so we're gonna be doing some code here, I'll get rid of that lovely comment and I'll need a decoder for this, so do let decoder equals PropertyListDecoder and then the URL for the plist is in the main bundle, so I'm just gonna get the URL and then explicitly unwrap it, since I already know it's there, so we'll just do let url equal, as it's in the main bundle, I'll just do B-U-N for Bundle tab and main.url and then forResource withExtension, the resource is Backgrounds withExtension, plist and there you can see it right here, Backgrounds.plist, now I know it's there, if it's not there, things have gone horribly wrong, so I'm just gonna put an exclamation point after that, since that is a optional and I'm gonna just explicitly unwrap it, I'll open the plist as data, the codable protocol reads data, this initializer for data does throw errors, so I'm gonna use an optional try here, so we'll do if let data equals try question mark Data contentsOf and that'll be the url.
Now once I have that data, I'm gonna send it through the decoder, the decoder also throws errors, so I'm gonna do the same if let try thing again, if let settings equals try decoder .decode and I just need this first one here, the type from, so the type is going to be this up here, my backgroundSettings and so I'm gonna put in here backgroundSettings .self and from my data, so I put data in here.
If that all works, I'm gonna have some settings, so now I can take inside that and set my color name, my background color and the like, so what I'll do for the color name is pretty straightforward, let colorName equal, since that's an integer, I'm gonna change it to a String, 'cause my color names are actually Color one, two, three, so I'm gonna do a String format: Color space percent i, since it's an integer comma settings.color and now I can have my colorName, so I can do view.
backgroundColor equals UIColor named colorName and finally background image and I already set the name of that image, so backgroundImage.image equals UIImage named settings.image. Alright, I've set my simulator for a IPhone eight plus, I'm gonna go ahead and run it on that and I get a lovely picture of a cake, which looks really nice on my light-colored background.
I'm gonna stop the app, as you see here in the Assets folder, I've got the cake, I've got a whole bunch of different colors, I've got some pretzels, so I'm gonna put the pretzels on Color three and so let's go ahead and go back to Backgrounds.plist, I'm gonna change cake to pretzels and number to three and then run that and the app changes, we get our pretzels on the darker background.
So many settings are easier to place in a property list, than within your app, it may change, but can be persistent when you reset your app, you can also write to the plist, that's similar to the JSON encoder, so take a look there for more information.