Chapter 23
Working with CloudKit
The most impressive people I know spent their time with their head down getting shit down for a long, long time.
- Sam Altman
Let's start with some history. In 2011, during Apple's annual Worldwide Developers Conference (WWDC), Steve Jobs introduced iCloud as a complementary feature for iOS 5 and OS X Lion. The announcement garnered significant attention, although it was not entirely unexpected. With iCloud, apps and games gained the ability to store data on the cloud and seamlessly synchronize it between Macs and iOS devices.
However, iCloud fell short in one aspect: serving as a cloud server.
Developers were restricted from utilizing iCloud for storing public data that could be shared among users. Its functionality was limited to facilitating data exchange exclusively between multiple devices owned by the same user. To illustrate this limitation, let's consider our Food Pin app as an example. With the classic version of iCloud, you would not be able to store your favorite restaurants publicly and make them available to other users of the app. The data stored on iCloud can only be accessed by you and cannot be shared with others.
During that time, if you desired to create a social app for data sharing among users, you had two options. The first was to develop your own custom backend server, complete with server-side APIs for data transfer and user authentication. Alternatively, you could rely on third-party cloud service providers like Firebase and Parse.
Note: Parse was a very popular cloud service at the time. But Facebook announced the demise of the service on January 28, 2016.
However, in 2014, Apple made significant improvements to iCloud's functionality, providing both developers and users with new and enhanced features. The introduction of CloudKit marked a substantial leap forward from its predecessor and opened up vast possibilities for developers. With CloudKit, it became much easier to develop social networking apps or incorporate social sharing features.
But what if you have a web app and want to access the same data stored on iCloud as your iOS app? Apple took CloudKit a step further by introducing CloudKit web services, also known as CloudKit JS. This technology leverages a JavaScript library, allowing you to develop a web app that can access the same data on iCloud as your native app.
During WWDC 2016, Apple made a significant announcement regarding the introduction of the Shared Database feature. This update expanded the capabilities of CloudKit, enabling developers to not only store data publicly or privately but also share data with specific groups of users.
CloudKit simplifies the development process for developers by eliminating the requirement to create and maintain their own server solutions. With minimal setup and coding, CloudKit empowers your app to store various types of data, including structured data and assets, in the cloud. This streamlined approach saves time and effort, allowing developers to leverage the power of cloud storage without the need for extensive backend development.
Best of all, you can get started with CloudKit for free (with limits). It starts with:
- 10GB for assets (e.g. images)
- 100MB for database
- 2GB for data transfer
As your app becomes more popular, the CloudKit storage grows with you and adds an additional 250MB for every single user. For each developer account, you can scale all the way up to the following limits:
- 1PB assets
- 10TB database
- 200TB data transfer
That's a massive amount of free storage and is sufficient for the vast majority of apps. According to Apple, the storage should be enough for about 10 million free users.
With CloudKit, we were able to focus on building our app and even squeeze in a few extras.
- Hipstamatic
In this chapter, my focus will be on guiding you through the integration of iCloud using the CloudKit framework. However, our emphasis will solely be on the Public database. Similar to the web views discussed in the previous chapter, SwiftUI does not provide specific CloudKit components. Nevertheless, I will demonstrate how to incorporate CloudKit APIs into SwiftUI projects. Specifically, you will learn how to retrieve and manage records within the iCloud database. We will enhance the app to allow users to anonymously share their favorite restaurants and upload them to the public database of iCloud. All users will then be able to view these shared favorites in the Discover tab.
There is a catch, though. To access CloudKit storage, you must enroll in the Apple Developer Program, which costs USD 99 per year. Apple restricts CloudKit usage to paid developers only. If you are serious about creating your app, it's time to enroll in the program and start building some CloudKit-based apps.
To access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.