Framework

CloudKit JS

Provide access from your web app to your CloudKit app’s containers and databases.

Overview

Use CloudKit JS to build a web interface that lets users access the same public and private databases as your CloudKit app running on iOS or macOS. You must have an existing CloudKit app and enable web services to use CloudKit JS.

Before You Begin

Set up your app’s containers and configure CloudKit JS.

  1. Create your app’s containers and schema.

    If you are new to CloudKit, start by reading CloudKit Quick Start. You’ll use Xcode to create your app’s containers and use CloudKit Dashboard to view the containers. Then create an iOS or Mac app that uses CloudKit to store your app’s data.

  2. In CloudKit Dashboard, enable web services by creating either an API token or server-to-server key.

    Use an API Token from a website or an embedded web view in a native app, or when you need to authenticate the user. To create an API token, read Accessing CloudKit Using an API Token in CloudKit Web Services Reference.

    Use a server-to-server key to access the public database from a server process or script as an administrator. To create a server-to-server key, read Accessing CloudKit Using a Server-to-Server Key in CloudKit Web Services Reference. See CloudKit Catalog: An Introduction to CloudKit (Cocoa and JavaScript) for a JavaScript sample that uses a server-to-server key.

  3. Embed CloudKit JS in your webpage.

    Embed CloudKit JS in your webpage using the script tag and link to Apple’s hosted version of CloudKit JS at https://cdn.apple-cloudkit.com/ck/2/cloudkit.js.

    <script src="https://cdn.apple-cloudkit.com/ck/2/cloudkit.js">
    
  4. Enable JavaScript strict mode.

    To enable strict mode for an entire script, put "use strict" before any other statements.

    "use strict";
    
  5. Configure CloudKit JS.

    Use the CloudKit.configure method to provide information about your app’s containers to CloudKit JS. Also, specify whether to use the development or production environment. See CloudKit for an example, and see CloudKit JS Data Types for details on the CloudKit.CloudKitConfig properties you can set.

Now you can use the CloudKit.getDefaultContainer method in your JavaScript code to get the app container (CloudKit.Container) and its database objects (CloudKit.Database).

Next Steps

To learn CloudKit JS, download the CloudKit Catalog: An Introduction to CloudKit (Cocoa and JavaScript) sample code project and refer to the CloudKit JS class reference documents for API details. For the hosted version of this sample code project, which allows you to execute CloudKit JS code and see the CloudKit server responses, go to CloudKit Catalog.

The following resources provide more information about CloudKit:

Topics

Classes

CloudKit

Use the CloudKit namespace to configure CloudKit JS, and to access app containers and global constants.

CloudKit.CKError

A CloudKit.CKError object encapsulates an error that may occur when you use CloudKit JS. This includes CloudKit server errors and local errors.

CloudKit.Container

A CloudKit.Container object provides access to an app container, and through the app container, access to its databases. It also contains methods for authenticating and fetching users.

CloudKit.Database

A CloudKit.Database object represents a public or private database in an app container.

CloudKit.DatabaseChangesResponse

A CloudKit.DatabaseChangesResponse object encapsulates the results of fetching changed record zones in a database.

CloudKit.Notification

A CloudKit.Notification object represents a push notification that was sent to your app. Notifications are triggered by subscriptions that you save to the database. To subscribe to record changes and handle push notifications, see the saveSubscription method in CloudKit.Database.

CloudKit.QueryNotification

A CloudKit.QueryNotification object represents a push notification that was generated by a subscription object. A query notification is triggered by subscriptions where the subscriptionType key is query. Use a CloudKit.QueryNotification object to get information about the record that changed. To create query subscriptions and handle push notifications, see the saveSubscriptions method in CloudKit.Database.

CloudKit.QueryResponse

A CloudKit.QueryResponse object encapsulates the results of using a query to fetch records

CloudKit.RecordInfosResponse

A CloudKit.RecordInfosResponse object encapsulates the results of fetching information about records in general and shared records in particular.

CloudKit.RecordsBatchBuilder

A CloudKit.RecordsBatchBuilder object encapsulates the results of changes to multiple records in a single database operation.

CloudKit.RecordsResponse

A CloudKit.RecordsResponse object encapsulates the results of fetching records.

CloudKit.RecordZoneChangesResponse

The CloudKit.RecordZoneChangesResponse object encapsulates the results of fetching changes to one or more record zones.

CloudKit.RecordZoneNotification

A CloudKit.RecordZoneNotification object represents a push notification that was caused by changes to the contents of a record zone. A zone notification is triggered by subscriptions where the subscriptionType key is zone. Use a CloudKit.RecordZoneNotification object to get information about the record that changed. To create zone subscriptions and handle push notifications, see the saveSubscriptions method in CloudKit.Database.

CloudKit.RecordZonesResponse

A CloudKit.RecordZonesResponse object encapsulates the results of database operations on a record zone.

CloudKit.Response

The CloudKit.Response class is an abstract superclass for subclasses that encapsulate the response from server requests. Don’t create instances of this class. Instances of subclasses are returned by methods in the CloudKit.Container and CloudKit.Database classes. Most of these methods return a Promise object that resolves to a subclass of CloudKit.Response if the operation is successful.

CloudKit.ShareRecordType

Display information about the record type of a shared record.

CloudKit.SubscriptionsResponse

A CloudKit.SubscriptionsResponse object encapsulates the results of database operations on subscriptions.

CloudKit.UserIdentitiesResponse

A CloudKit.UserIdentitiesResponse object encapsulates the results of fetching user identities.

Reference

CloudKit JS Data Types

This document describes the CloudKit JS data types that are not described in individual class reference documents.