iCloud Public Database Query, Download, and Report (2/2)
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:
- Setup and Upload
- 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
- Define a variable for the array books: [CKRecord] = []
- Verify that iCloud is set up for download
- Instantiate a reference to the public database and the record type ‘Book’
- Construct the CKQuery for record type ‘Book’
- Sort the query by creationDate (date of upload, so the newest uploaded will be on top)
- Specify which fields are to be included in the query
- Add the query to the public database
- Display the query result in the Discover table view
- Define the row swipe gesture actions: Download and Report
- 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!