What is CloudKit and its overview?

khusboo

The CloudKit framework provides a mechanism to store data of app on iCloud so that user can access the data on multiple devices.It’s a good option for the app that deals with lots of data.The CloudKit relies on the presence of the network.

Data is segregated and encapsulated in partitions called containers in iCloud.Each app has one default containers and can create custom containers if needed.The containers of one app cannot be accessed by another app.However, an app can share their containers, and also one single container can be shared by multiple apps.

A container contains public and private database and the app has access to both the database.
The public database can be accessed by all the users of the app and it’s for storing the user and app data that is shared by all the instances of the app.To write in the public database, the user must login with their iCloud credentials.A private database is created for each user of the app.
The user has to login with their iCloud credentials to read and write data to the private database.
All the transaction in CloudKit is done through records.A record is a dictionary of key-value pairs that represent the data that the user want to save on iCloud.

There are two main components of CloudKit are
1.A web dashboard: to manage records type and any public data.
2.A set of API’s: to transfer data between the iCloud and the device.

The advantages of CloudKit are simplicity, secure and cost.

Enabling CloudKit

To access CloudKit the app must be provisioned and code signed.
The following steps need to be done to enable iCloud and select CloudKit:

1)In the capabilities tab pane, turn the switch on for iCloud.With this Xcode provisions your app to use iCloud.
2)Select the CloudKit checkbox.This allows Xcode to create a default CloudKit container based on the bundle ID and adds CloudKit framework to your project.

2_enable_cloudkit_2xCloudKit Dashboard

CloudKit schema and records are managed using the CloudKit dashboard.
The following are the steps to sign in to CloudKit dashboard:
1)In the iCloud settings, capabilities pane, click CloudKit dashboard.Or we can open it through this link https://icloud.developer.apple.com/dashboard.
2)Enter Apple ID credentials and click sign in, if necessary.

All the containers for all the team that you belong appear in the container pop-up menu in the left upper corner of the window.

2_cloudkit_dashboard_hom

The Cloud Dashboard contains four section:
1)Schema: It contains the high-level objects of the CloudKit container like Records types, Security roles, Subscription Types.
2)Public Data: It allows to add data to, or search for data in the Public Database.
3)Private Data: It allows to add data to, or search for data in the Private Database.
4)Admin: It provides the ability to configure the dashboard permissions for the team members of the app.

Add Containers to app

We can use the existing container ID used by another app or we can create a new one.
The following are the steps to add a container to the app:
1)Select “Specify custom containers” in the Capabilities pane under the iCloud settings.

3_specifycontainers1_2x
2)Click refresh button below the Containers table to download containers from your developer account that are used by others apps, if necessary.
3)Select the Checkbox in the row of the Container ID that you want to add.Xcode updates the list of container IDs in the entitlements file.

3_specifycontainers2_2x

Create Custom Containers

To create a custom container,t he following are the steps:
1)Select “Specify custom containers”, if user default containers are selected.
2)Click the Add(+) button at the bottom of the table.
3)Enter an identifier for the container, in the dialog that appears.

A container ID begins with icloud. followed by a string in reverse DNS notation

3_addicloudcontainerdialog_2x

4)Click “Ok”
Xcode adds the new container ID to the Xcode project entitlements file and to your developer account.

Creating Database Schema By Saving Records

A CloudKit schema consists of one or more records types that have a name, fields, and other metadata.
Asset type is used for bulk data that is stored separately from records.
A Location type for querying geographical coordinates efficiently.
A reference type to represent one-to-one and one-to-many relationships among records.
The maximum size of a record is 1 MB.
The table shows the possible fields types, as the appear on Cloud dashboard and their equivalent CloudKit framework classes.

Capture 45

Create Records Programmatically

To create a record programmatically,the steps that follow are:
1)Create a record ID specifying a unique record name.
#Swift code
let artwkRecordID = CKRecordID(recordName: “115”) //Swift code

#Objective-C code
CKRecordID *artwkRecordID = [[CKRecordID alloc] initWithRecordName:@”115″];

2)Create a record object.

#Swift code
let artwkRecord = CKRecord(recordType: “Artwork”, recordID: artwkRecordID)

#Objective-C code
CKRecord *artwkRecord = [[CKRecord alloc] initWithRecordType:@”Artwork” recordID:artwkRecordID];

3)Set the record’s fields.

#Swift code

artwkRecord[“title”] = “MacKerricher State Park” as NSString
artwkRecord[“artist”] = “Mei Chen” as NSString
artwkRecord[“address”] = “Fort Bragg, CA” as NSString
#Objective-C code

artwkRecord[@”title” ] = @”MacKerricher State Park”;
artwkRecord[@”artist”] = @”Mei Chen”;
artwkRecord[@”address”] = @”Fort Bragg, CA”;

Save Records

To save the records ,the steps that follow are:
1)Get the database in your app’s default container.

To get the public database:

#Swift code
let myappContainer = CKContainer.default()
let publicDatabase = myappContainer.publicCloudDatabase

#Objective-C code

CKContainer *myappContainer = [CKContainer defaultContainer];
CKDatabase *publicDatabase = [myappContainer publicCloudDatabase];

To get the private database:

#Swift code
let myappContainer = CKContainer.default()
let privateDatabase = myappContainer.privateDatabase

#Objective-C code
CKContainer *myappContainer = [CKContainer defaultContainer];
CKDatabase *privateDatabase = [myappContainer privateCloudDatabase];
To get a custom container:

#Swift code
let myappContainer = CKContainer(identifier: “iCloud.com.example.ajohnson.GalleryShared”)

#Objective-C code
CKContainer *myappContainer = [CKContainer containerWithIdentifier:@”iCloud.com.example.ajohnson.GalleryShared”];
2)Save the record.

#Swift code
publicDatabase.save(artwkRecord) {
(record, error) in
if let error = error {
// Insert error handling
return
}
// Insert successfully saved record code
}
#Objective-C code
[publicDatabase saveRecord:artwkRecord completionHandler:^(CKRecord *artwkRecord, NSError *error){
if (!error) {
// Insert successfully saved record code
}
else {
// Insert error handling
}
}];

If the records doesn’t exist ,CloudKit framework sets it with the fields you set.

# NOTE:Enter iCloud credentials before running your app.

View Records types using the CloudKit dashboard

To view the record types,t he steps that follows are:
1)Sign-in to CloudKit dashboard.
2)In the upper left corner choose the container used by your app from the pop-up menu
3)In the left columns, click the records type.
4)In the second column,3_view_record_types_2x select a record type.

Enable Metadata ID Indexes

All metadata ID indexes are disabled by default for all record types created with just in time schema.The record ID query index need to be enabled to view associated records in the CloudKit dashboard.

The steps to enable metadata ID index are:
1)Click Records types in the left column, and select a record type.
2)Click the index disclosure triangle under the Metadata indexes.
3)Select the query box in the record ID row and click save.

3_enable_record_id_query_index_2x

View Records using CloudKit Dashboard

The steps to view a record are:
1)In the left column of CloudKit Dashboard, click Default Zone under Public Data or Private data.Records appear in the second column.
2)Click a record, in the second column.The record-key-value pairs appear in the detail area on the right.

With the CloudKit, you can take your apps to advance level of user experience.It provides a low-cost option to store and share app data using your user’s iCloud account as a backend storage service.It can also be used for web and server applications.

Google Certified Agency

The following are the reference links for CloudKit

1)https://developer.apple.com/reference/cloudkit
2)https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/EnablingiCloudandConfiguringCloudKit/EnablingiCloudandConfiguringCloudKit.html
3)https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/CreatingaSchemabySavingRecords/CreatingaSchemabySavingRecords.html
4)https://www.raywenderlich.com/134694/cloudkit-tutorial-getting-started
5)http://www.appcoda.com/cloudkit-introduction-tutorial/

 

 

 

 

about the author

khusboo