CloudKit, Apple’s new remote data storage service for apps based on iCloud, provides a low-cost option to store and share app data using user’s iCloud account as a back-end storage service.
There are two parts to CloudKit:
1. A web dashboard to manage the record types along with any public data.
2. A set of APIs to transfer data between iCloud and the device.
The answers are three: simplicity, trust, and cost.
Simplicity: Unlike other backend solutions, CloudKit requires a little setup. Simply registering for the iOS Developer Program makes you eligible to use CloudKit – you don’t have to register for additional services or create new accounts.There’s no need to download additional libraries and configure them. CloudKit is imported like any other iOS framework.
Trust: Another benefit to CloudKit is that users can trust the privacy and security of their data by trusting Apple, rather than app developers since CloudKit insulates the users’ data from you.
Cost: Getting Started with CloudKit for free. CloudKit provides a generous amount of free public storage and data transfer to help you get started. Sign in to the CloudKit Dashboard to view your quota and project usage.
CloudKit’s Free usage:
CloudKit provides a generous amount of free public storage and data transfer to help you get started.
- 10 GB Asset storage
- 100 MB Database storage
- 2 GB Data transfer
- 40 Requests per seconds
What is needed for CloudKit?
- Needed Xcode 6 or more installed in mac
- Membership in the Apple Developer Program
- Permission to create code signing and provisioning assets in Member Centre
Steps to Start with CloudKit in Xcode project:
Step 1 : Move to capabilities – switch on iCloud service
Step 2 : Asks for the apple id associated with developer account
Step 3 : When you select CloudKit, Xcode creates a default container ID based on the bundle ID.
Because you can’t delete iCloud containers, verify that your bundle ID is correct in the General pane in Xcode before selecting CloudKit.Xcode has you covered in this area – via the Capabilities tab of your project settings file:
Flicking the switch will enable iCloud for your app
Note that the default settings just enable the iCloud Key-Value store and that you need to check the CloudKit checkbox to link against the correct framework and configure the entitlements appropriately
Fundamental CloudKit Objects:
There are 7 different fundamental objects in CloudKit.
CKContainer : A container is like a sandbox. An application can only use the resources inside its container. The container is located at the very outer border and each application has one and only one separate container.
CKDatabase : A database is a place that you put all your data. There are two different kinds of databases: private and public. The private database is where you store sensitive data, like user’s information. The public database is where you store shared data.
CKRecord : A record is a piece of data inside your database. It is stored as a key-value pair. For now, you can save NSString, NSNumber,NSData, NSDate, CLLocation, CKReference, and CKAsset, as well as arrays of all the types listed above.
CKRecordZone : Records are not stored scattered in a database, they are located in record zones. Every application has a default record zone, and you can also have your own custom record zones.
CKRecordIdentifier : The unique label of a record, used for locating a particular record.
CKReference : Reference is like the relationship in an RDBMS. In our check-in example, there may be many people checked in at the same place, so we’ll need to establish a reference between places and check-ins.
CKAsset : Assets are resources, like binary files or bulk data. For example, a user’s picture should be stored as an asset.
- The container is the top-level Storage object in the world of CloudKit, and by default, each app has its own, independent container.
- It is possible that two apps signed by the same developer share the same container – permitting sharing between iOS and OSX, as well as across multiple apps on the same platform.
- Container object coordinates all interactions between your app and the server.
- More apps by the same developer can share container
Creating custom Container:
1. If “Use default container” is selected, select “Specify custom containers.”
2. Click the Add button (+) at the bottom of the table.
3. In the dialogue that appears, enter an identifier for the container you want to add. The format of Container ID: A container ID begins with iCloud. followed by a string in reverse DNS notation
4. Click OK. Xcode adds the new container ID to the Xcode project entitlements file and to Member Centre.
Editing container names and viewing them :
You can view all the container IDs for your team in the iCloud settings or Member Centre. In Member Centre, you can also add containers and edit the name of containers.
To view container IDs in Member Centre
1. In Member Centre, select Certificates, Identifiers & Profiles.
2. Under Identifiers, select iCloud Containers.
Each container has exactly two databases: one public and one private.
The public database is shared between all users of this container – everybody can access the data, and by default, everybody can write to it.
The private database is, as you might expect, completely private to the current user. This means that only the logged-in user has access to this data – they can’t choose to share part of it, nor can the developer take a look at a user’s private database.
This is an extremely powerful feature – offering top-notch privacy straight out of the box.
The API is very clear about which database you are interacting with – all operations are performed on a database, and you use either the
- privateCloudDatabase or publicCloudDatabase
- properties on your CKContainer container object.
Access CloudKit Dashboard
Use CloudKit Dashboard to manage your CloudKit container schema and records. The schema describes the organization of records, fields, and relationships in a database.A record is an instance of a record type. In a relational database, a record type corresponds to a table and a record corresponds to a row in a table.
To sign in to CloudKit Dashboard.
1.In the iCloud settings in the Capabilities pane, click CloudKit Dashboard. Alternatively, go to icloud.developer.apple.com/dashboard.
2.If necessary, enter your Apple ID credentials and click Sign In. All the containers for all the teams you belong to appear in the container pop-up menu in the upper-left corner of the window.
To sign out, choose Sign Out from the account pop-up menu in the upper-right corner of the window.
Creating and Saving Records in CloudKit Dashboard:
– When you select Default Zone under Public Data, the record type should already be selected.
– Create a new record either by clicking New Record or by clicking the plus button at the top.
– Enter whatever you want for the record’s Description and Title fields.
– Click Save in the bottom right to create the record. You’ll see that your record has been assigned a unique record ID, Created date, Modified date, Created By identifier, and Modified By identifier.
– No matter what record type a record is based on, it will always have these five attributes.
Enter iCloud Credentials Before Running Your App:
In development, when you run your app through Xcode on a simulator or a device, you need to enter iCloud credentials to read records in the public database.In production, the default permissions allow non-authenticated users to read records in the public database but do not allow them to write records.Therefore, before you run your app and save records to the database, enter an iCloud account in Settings on iOS or System Preferences on a Mac. Also, enable iCloud Drive. Later, write the necessary error handling to present a dialogue to the user when iCloud credentials are needed to run your app in iOS Simulator, enter the iCloud credentials in iOS Simulator before you select the simulator and click the Run button in Xcode.You need to perform these steps for each iOS Simulator you select in the Scheme pop-up menu in Xcode.
To enter iCloud credentials in iOS Simulator
1. Choose Xcode > Open Developer Tool > iOS Simulator
2. In iOS Simulator, choose Hardware > Home.
3. Launch the Settings app and click iCloud.
4. Enter an Apple ID and password.
5. Click Sign In.Wait while iOS verifies the iCloud account.
6. To enable iCloud Drive, click the iCloud Drive switch.If the switch doesn’t appear, iCloud Drive is already enabled.
In the development and production environment, you can search for records that have string fields.
To search for records
1.In the left column of CloudKit Dashboard, click Default Zone under Public Data or Private Data.Records appear in the second column.
2. In the second column, click the search icon.A search field appears.
3. Enter text in the search field.CloudKit Dashboard sorts the records by the field values. If the record type doesn’t have a searchable field, “doesn’t have a searchable field.” text appears below the search field.
In the development and production environment, you can sort records by field.
To search for records
1. In the left column of CloudKit Dashboard, click Default Zone under Public Data or Private Data.Records appear in the second column.
2. From the “Sort by address” pop-up menu, select a field.CloudKit Dashboard sorts the records by the field values.
3. To change the order, from the “Sort by address” pop-up menu, select Ascending or Descending.
CloudKit is a nice addition to the backend service and causes that we may stop worrying about the backend side, if not for production solution, then at least for prototyping and testing ideas.
It gives us the ability to track the changes, synchronising assets and syncing data with a small effort. It is also nice replacement for mBaaS services, if you don’t need a cross-platform feature and if you want to stick just with iOS/OSX or web.
“Power is gained by Sharing knowledge, share your knowledge”