Join Todd Perkins for an in-depth discussion in this video Build the CloudKit project, part of iOS App Development: Apple iCloud API with iOS (2016).
- [Voiceover] So, let's create our CloudKit project. So, I'll click to create a new project from the welcome screen. Under iOS, Application, I'm going to choose Single View and click Next. I'll call this CloudKitExample. Leave everything else as it is including the language at Swift and Devices, iPhone and click Next. And, I'll save this in Chapter 03 cloudkit_project final. Again, if you have the exercise files, save it outside of this final folder. So I'll take Create and create the project.
I'm going to change the device to 6s instead of 6+ because it fits better on my screen. Now, the first step is to, with our project selected in the navigator, go to Capabilities and enable iCloud. Now, you'll have to choose your developer credentials there and then the entitlement gets added to your App ID. And then I'm going to choose CloudKit and the next step is to just build the user interface. Let's go to Main.storyboard and hide the document outline. And then, I'm going to need first a Text View.
This is just going to hold a list of objects. So, since CloudKit is a database, we're going to add records to the database. Now, this is going to be very simple. We're just going to have a one-field database and then we'll be able to add and remove those records. But, as we go through this, I'll show you how you can create multiple fields. So, I'll delete all this text and there's our text field right there. And then, I'm going to look for a Text Field. So we have our Text View above, our Text Field below that.
And I want two of those, so I'm going to Option, Drag. The first one's going to be for records we want to add and then this other one is for records we want to delete. So now I'll search for a button. And I'll change the button text to Add. So, we're going to use this to add a record. And then Option, Drag down. Delete. And then, Option, drag over here and we'll just make a Refresh button.
Now, as before, if I was making a project that I was going to release to the public, I would use pins, and I would make this beautiful. But since we want to focus on just iCloud code, I'm making this so that it looks okay in a compact layout. And sure enough, it does. So, I'll go back to the Any Any layout. And now, our goal is to connect these UI elements to our code. So, I'm going to hide the utility area and then I'll Option click ViewController.swift and bring that up on the right.
Now, I'll make a few extra lines and then just drag everything on in. So this is going to be called tView. So make sure Connection is an Outlet and the Type is UITextView. Hit Connect. There's our T View. And then we need the text for adding, so I'll call the tAdd. And then this one, I'm right clicking and dragging. Call it, tDelete. And then we need the Add and Delete methods. So, I'm going to change this connection to an Action. Call this, didTapAdd, that.
Some more lines here. Right click and drag from Delete in there. Again, Action and didTapDelete. And then, finally, for Refresh. Drag it in there, with a right click, change it to Action, didTapRefresh. Just like that, and Connect. So, there's our code. We'll return to the Standard Editor and return to ViewController.swift. And I'll go inside of these methods to make them look pretty. And this is our basic application and I'm actually going to run this in the simulator before going to manage our databases in the CloudKit dashboard.
Because, if we run it in the simulator, the entry in CloudKit dashboard will actually be created automatically and we won't have to create one manually. So, that's why I'm doing this now, to have Xcode do all of that work for us. So, here's our app running in the simulator. And we have that text fill at the top and the Add, Delete, and Refresh buttons. Looks like everything looks fine. So, I'm going to stop the application and now we're done with the base application. So, we made our user interface, we made the code connections, and we enabled CloudKit inside of our project settings.
- Working with iCloud key-value pairs
- Building an example app that accesses iCloud documents
- Saving and loading iCloud documents
- Fetching records with CloudKit