In order to support iCloud documents, you need to create a class that subclasses UIDocument. In this tutorial, see how that process works to create the subclass.
- [Instructor] Our next step is to create a UI document subclass. This class manages your saved data. When you create the subclass, you need to handle two methods that load and save data to the document. Let's look at how that works. I'm going to select ViewController.swift, press command N on the keyboard to create a new file, under IOS I'm going to choose cocoa touch class and click next.
We want this to be a subclass of UI document and I'll call the class CloudDoc. So I hit next and then I'll create it in the default location and there's my CloudDoc class. As I mentioned a second ago, we need to handle two methods for loading and saving data. Those two methods are load, we're going to handle load and that's going to receive it from contents of type and it throws an error, I'm going to delete the code inside of there.
I'll go right above it and I'm going to handle contents and this throws an error as well. So we have load from contents and then we have contents for type and this returns any object. So as you might guess, load deals with loading data from the document and contents deals with saving data. So in contents, we need to return the data that we want to save. The data that we want to save is whatever's inside of our text view.
So we need some way of referencing the text view inside of CloudDoc. Let's head over to view controller and we're just going to make a global property here, so I'll create global var and we're going to call this main VC and this is going to be of the type view controller and I'll add a question mark to make it an optional since we can't give it a value just yet. Let's give it a value inside view did load. So underneath the comment in there, I'm going to set main VC equal to self, so now we have this reference to our main view controller, so we just need to reference this global property and we can grab the tView from that property inside of CloudDocs.
So I'll save the file, head over to CloudDoc, and in contents for type I want to return the text that's in the text field and I'm going to return it as a data object. So we'll do return and then we're going to return main VC tView dot text and then we're going to convert that string value to a data object by calling the strings method data using encoding. So that's this top one right here.
So I'll press return on that and the encoding's going to be dot UTF eight. I'll close out the parentheses and then I'm not able to return an optional from this method, so I'm going to force unwrap these. Now, of course, in a production application you want to do all the checks to make sure that everything's non-nil before returning the value, but since we're just building a sample application, I'm force unwrapping for speed and convenience. So let's head over to load from contents and then what we're going to do is just take that data and then unwrap it.
So the data's coming in through this contents parameter which is an any object. We're going to treat it as a data object here. So we'll define let data and it's of the type data, so we'll just add that right here and I'm going to set it equal to contents as data. Now it is possible that this conversion will fail. Maybe when the app opens up for the first time.
So what we want to do is make sure that we have some way to handle that. So right before the let statement, I'm going to type guard. I'll change that exclamation point to a question mark and then after data, else and I'll print out error loading data and then return. So now we just have to use that data constant. So below the guard statement, I'm going to put the data inside of the text view. So, main VC dot tView dot text equals and then I need to convert that data into a string, so I'll call the string constructor and we want the one that receives data.
So I'm going to type out a D to narrow down that code hinting and I'm finding this one, data and encoding, so the data is going to be that data constant we just created, I'll tab over to encoding, and just like above when we encoded the data we use UTF eight string encoding so dot UTF eight here and that's it. So we save the file with contents for type, we take the string that's inside of the text view and we turn it into a data object and that saves that data and then we have this load method when we're ready to load the data and that takes whatever comes in as contents tries to convert it to a data object and once it's converted successfully then we put that data inside of our text view as a string.
- iCloud key-value pairs
- Building a key-value pair project
- Understanding iCloud documents
- Building a iCloud document app
- Creating a subclass of UIDocument
- Saving to iCloud Drive
- Building a CloudKit project
- Using the CloudKit Dashboard
- Fetching records from a database