Provide access from your web app to your CloudKit app’s containers and databases.
Language
- JavaScript
SDK
- CloudKit JS 1.0+
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.
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.
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.
Embed CloudKit JS in your webpage.
Embed CloudKit JS in your webpage using the
scripttag and link to Apple’s hosted version of CloudKit JS athttps://cdn.apple-cloudkit.com/ck/2/cloudkit.js.<script src="https://cdn.apple-cloudkit.com/ck/2/cloudkit.js">Note
The CloudKit JS version number is in the URL. For example,
2specifies CloudKit JS 2.0.Enable JavaScript strict mode.
To enable strict mode for an entire script, put "use strict" before any other statements.
"use strict";Configure CloudKit JS.
Use the
CloudKit.configuremethod to provide information about your app’s containers to CloudKit JS. Also, specify whether to use the development or production environment. SeeCloudKitfor an example, and see CloudKit JS Data Types for details on theCloudKit.CloudKitConfigproperties 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:
CloudKit Quick Start gets you started creating a CloudKit native app.
CloudKit teaches you how to write native app code.
CloudKit Web Services Reference describes the HTTP interface to CloudKit containers and databases.
iCloud Design Guide provides an overview of all the iCloud services available to apps submitted to the App Store or Mac App Store.