iCloud Public Database Setup and Upload (1/2)

Mazen Kilani
If let swift = Programming!
4 min readApr 19, 2021

Share Your Interesting Data Records with Users of the App at Apple’s Public Database

Scenario

Our app will allow users to upload their app records to Apple’s public iCloud database, so all app users can share their selected data (books, recipes, anecdotes, jokes, etc.). The users should be able to upload, query, and download from the public database, as well as report any objectionable posted material by other users (as per Apple’s User Generated Content policy).

Technique

We shall utilise Xcode CloudKit Dashboard interface to set up the app iCloud.com containers for Development and Production of the public database. Then create the Record Type (e.g., ‘Book’) that will include the data fields and the indexes, which are needed for the query and download of public records. We call the public database sharing feature ‘Discover’ in our app.

Audience

The article is for Swift developers who seek complete, integrated, proven, code-centric solutions to speed up their development projects. The iCloud code used in this article can be added, with minimal customisation, to any part of your app, but it usually resides in the New and Edit views.

App Model

We based the article on the AppsGym Books model app, which is published on Apple’s App Store (as 8Books) for free, no ads. You can review the app and download the complete Xcode project on AppsGym.com, also free.

Multi-Part Series of Articles

The iCloud operations are relatively complex and diverse. Hence, we shall use two articles to segregate the different functions, as follows:

  1. Setup and Upload (this article)
  2. Query, Download, and Report

User Interfaces

We provide the facility to upload an Book record to iCloud in 2 views: New Book and Edit Book.

The New Book view presents a Yes/No option for the user to upload the book record to the public iCloud, upon saving the book record to the app database.

The Edit Book view shows the current status of the Book shared, and also allows the user to share (if not shared already) upon saving.

CloudKit Setup

Setting up iCloud Public Database is a detailed process and best covered in Apple’s documentation. This tutorial assumes knowledge of the process and ability to login to CloudKit and establish a Public Database _defaultZone record called ‘Books’.

Please refer to Apple’s CloudKit documentation for the process details: CloudKit Documentation

Note: The Setup Steps below use screenshots of the already created Books app record ‘Book’, used in the Discover table view and cell, and show the result of the public database _defaultZone ‘Book’ type record.

Setup Steps

Add the CloudKit capability to the app. Navigate to the project Signing & Capabilities, search for iCloud, then add it to your project. Switch on CloudKit.

Login to CloudKit Dashboard using your developer account credentials. The CloudKit Dashboard will show the Development and Production environments. We start in the Development environment, where we establish the Schema of the new Record Type ‘Book’ (or whatever name you wish to use for your app).

We select Public Database, _defaultZone and add a new Type called ‘Book’.

Then, we add our Custom Fields to the Type ‘Book’.

We define the indexes for the new Type ‘Book’ for the custom fields we wish to use in our queries.

We can now access the public database _defaultZone record type ‘Book’ in our app to upload (in New and Edit views) and query (in Discover view) the public database records.

Upload Logic

NewBookTableViewController.swift contains the logic to upload a new (book) entity record to Discover iCloud Public Database.

The steps are as follows:

  1. Define variables to store user input for the isDiscoverYesButtonTapped
  2. Define a variable for the array recordsToDiscoverArray: [CKRecord] = []
  3. Verify that iCloud is set up for upload
  4. Instantiate a reference to the public database and the record type ‘Book’
  5. Construct the recordToDiscover by setObject commands
  6. Scale the book image for optimised upload
  7. Define the saveOperation
  8. Add the saveOperation to the public database.

Upload Code

The Swift code snippets below show the complete Share to Discover logic in NewBookTableViewController.swift. If you want the Xcode project template, you can download it, totally free, on AppsGym.com.

class Cloud, enum RecordTypes, enum CloudRecord

NewBookTableViewController.swift discoverYesNoButtonsToggle(..)

NewBookTableViewController.swift viewDidLoad()

NewBookTableViewController.swift saveRecordToCoreData()

NewBookTableViewController.swift saveRecordToDiscover()

The article covered the complete setup, logic, and code to successfully upload an app user-selected record, to share with other users in the app public database. Hope it proves beneficial in your app. Thanks for reading!

--

--

Mazen Kilani
If let swift = Programming!

I published 47 Swift iOS apps/games and 2 Flutter Android apps. I share complete Xcode projects, free (no ads), at AppsGym.com.