Building a Shopping List Application With CloudKit: Sharing Shopping Items

In the previous tutorial of this series, you added the ability to create, update and remove shopping lists from on your iCloud-powered shopping-list app. In the final tutorial of the series, you'll make use of CKShare to share a specific shopping list item with another user. 

In this series, you have worked with private databases, as well as learning about public databases. However, until WWDC 2016, when Apple introduced CKShare, there was no proper way for apps to share data. Private databases are only available to users who are logged in, whereas public databases are designed for public content and allow anyone to view records. When using some of Apple’s very own iCloud apps, however, such as Pages, Keynote or Notes, you may have noticed by selecting the share button, the ability to invite other users to access your data. 

In this post, I'll show you how to share content selectively.


In this tutorial, you'll give your app the same ability, so users can collaborate with you on a shared shopping list item. 


Remember that I will be using Xcode 9 and Swift 3. If you are using an older version of Xcode, then keep in mind that you are using a different version of the Swift programming language.

In this tutorial, we continue where we left off in the third tutorial of this series. You can download or clone the project from GitHub.

About Databases

Apple provides three types of databases: public, private, and shared. A public database is where you make available content that everyone can access, all within the default zone. Users don’t need even to be authenticated with iCloud to view the content, but they can't write changes. 

A private database you should be familiar with already, as the shopping app has been leveraging the private database to store records that the public cannot access. Right now, all of your shopping lists are private lists. 

A shared database is Apple’s shared database window into your private database, which grants users access to read or write via CKShare

It's important to note that the shared database resides on the invited user’s account and is a portal to the owner’s private database, with the CKShare being a particular type of CKRecord that is attached to the CKRecord. 

Read the complete article, exclusively at Tuts+.
Doron KatzCloudKit