In order to support iCloud key-value storage in your applications, you'll need to setup your app to support iCloud key-value storage features. See how the process works in this tutorial.
- [Instructor] So now let's create our key value pair Xcode project. So I'm going to create a new project from the welcome screen. And of course you can always go to file, new, project, or use the keyboard shortcut to create one as well. Under IOS, I'm going to choose Single View Application from the template menu and click next. I'll call this product key value. Make sure that you set a team. You'll need to have a developer team set up in order to use iCloud features for your application.
So you'll need a developer account with Apple, come here, log into it. You can log in through preferences in the accounts menu if you need to log into your account. So you should have your development team selected there. And I'm going to leave everything else as it is and hit next. I'll save this in exercise files, Chapter 1 creating project, final. If you're following along with the exercise files, save your project outside of the final folder in here. I'm going to save in the final folder and then hit create.
So I've created my project and I can see that my team is set up and I have my bundle identifier. And what I want to do first is head over to the capabilities tab and in here I'm going to enable iCloud for my project. So I'm going to click the slider to enable iCloud, and I want to check key value storage and only key value storage. Now, once we've done this, our app is ready to use iCloud. And now we're going to build a simple interface to demonstrate how iCloud key value storage works.
Remember, this interface is not about building a sample application and adding a whole bunch of extra code. I want to keep the interface and code as simple as possible so that we can focus on just using iCloud in an application. So here I am in my storyboard. So I've clicked over to main.storyboard in the project navigator. I'm going to make sure that I'm testing on iPhone 7. And I have view as iPhone 7 set up.
This is going to enable us to create a very simple user interface. And we won't have to use auto-layout to manage our layout, we're just going to test on an iPhone 7, we're going to view it as an iPhone 7. So let's filter, in the object library, for a text field. And I'm going to drag out two of these. So one here at the top like that. And maybe I'll just bring it in a little bit. Then I'm going to option drag down. So now we have two text fields right there.
And then I want two buttons. So I'll filter for button in the object library. And then push a button to the right of the top text field. I'll write save as the text for that button. Option drag down to make a copy. And this button will be load. So the way this is going to work is we'll type some text inside of the top text field, and then we'll hit save. And that data's going to be saved to iCloud key value storage. And then we're going to hit the load button, and it's going to load the data from the key value storage.
So we'll be able to write this in another device. Hit save. And then we should hit load in the simulator, and it should load into this field. In other words the data from the other device should be loading into the simulator and vice versa. The next step is to connect these text fields and buttons to our code. So what I'm going to do is option click, view controller.swift... I'm going to hide the document outline. And then I'm going to hide the utility area and then right click and drag from these objects into my code to make the connections.
So I'll right click and drag from the top text field. And when I did that, it actually selected the UI view. So I'm going to cancel that out. Deselect everything by clicking in this white space, and then right click and drag again. It looks like I have the text field selected now. I'll drop that right underneath my class declaration. I have outlet for the connection, type is UI text field, and we'll call this tSave. Hit connect. And the same thing for the load text field. Right click and drag right underneath the previous code.
Call this tLoad. Make sure that you have outlet, UI text field. Hit connect. There we go. We've connected our text fields. Now we need to connect the save and load buttons. Right click and drag from the save button, and I'm going to go beneath viewDidLoad. We want an action for this because we want to respond to that button press. We'll call this didTapSave. And hit connect. There's our method right there.
And then for load, the same thing. Right click and drag right underneath didTapSave. We'll call this didTapLoad. And we don't want an outlet, we want an action. And it looks good. I'll hit connect. So we have didTapSave, didTapLoad, and then I'm going to create one extra method for later on. And so this is going to be called loadData. And I'm going to call this method inside of didTapLoad. So loadData.
And that should be it. So we've created our project, we've enabled iCloud in our project settings. So if I return to the standard editor, you can see that right here under my key value target. I have enabled iCloud and checked the box for key value storage. We've created a very basic user interface for demonstrating this technique. And finally, we connected those UI objects to our code.
- 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