iCloud Public Database Query, Download, and Report (2/2)

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

Query the App Public Database and Download or Report Users Shared Data

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 a dedicated Discover (or whatever name you call it) view.

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
  2. Query, Download, and Report (this article)

User Interfaces

DiscoverTableViewController.swift controls the layout, order, and navigation of the UITableViewController to show public database books by upload date (the newest upload appears on top).

The view allows a swipe gesture (right to left) showing a menu of Download and Report. The Download action will download the public cloud record to the app. The Report action will launch an email window, where the user can report an inappropriate record, as per Apple’s User Generated Content rules.

DiscoverTableViewCell.swift contains the cell @IBOutlets and functions.

The Discover.storyboard contains the storyboard for the Discover view, and links to the DiscoverTableViewCell.

Logic

  1. Define a variable for the array books: [CKRecord] = []
  2. Verify that iCloud is set up for download
  3. Instantiate a reference to the public database and the record type ‘Book’
  4. Construct the CKQuery for record type ‘Book’
  5. Sort the query by creationDate (date of upload, so the newest uploaded will be on top)
  6. Specify which fields are to be included in the query
  7. Add the query to the public database
  8. Display the query result in the Discover table view
  9. Define the row swipe gesture actions: Download and Report
  10. When the user selects Download, execute the downloadFromCloud(..) function

Code

ICloud.swift

DiscoverTableViewCell.swift

DiscoverTableViewController.swift Initialise Variables

DiscoverTableViewController.swift viewDidLoad()

DiscoverTableViewController.swift viewWillAppear()

DiscoverTableViewController.swift getRecordsFromDiscover()

DiscoverTableViewController.swift TABLE VIEW

DiscoverTableViewController.swift Download and Report Swipe Actions

DiscoverTableViewController.swift downloadFromCloud()

DiscoverTableViewController.swift mailComposeController(..)

DiscoverTableViewController.swift Helper

The article covered the complete logic and code to query the app public database, then allow the user to download, or report, a specific record. Hope you benefit from it 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.