NEW: Learn SwiftUI with my free YouTube video series! >>

< Overview   Next: Recording from the microphone with AVAudioRecorder >

Setting up

As I write these initial words, I already know this is going to be one of the most expansive and useful Hacking with Swift tutorials to date. We're going to be using CloudKit to load and save user data, we'll read from the microphone using AVAudioRecorder, we'll add UIStackView and NSAttributedString for great layout, and we're going to tie in push messaging for dynamic updates.

This tutorial is going to show you CloudKit to a depth you won't see much elsewhere. Yes, we're going to be loading and saving text data, but we're also going to be loading and saving binary data, registering for updates, and delivering push messages – in short, we're going to be covering the majority of CloudKit in one project, and you'll learn a huge amount along the way. We're even going to be covering the simple and advanced methods of working with CloudKit, because it gives a much better user experience.

As you know, my usual plan is to pick which technologies I want to teach, then strap a real project around it. In this case, the project is called "What's that Whistle?" It's an app where users can whistle or hum into their microphone, and upload it to iCloud. Other users can then download whistles and try to identify what song it's from. This is a genuinely useful app: think how often you know how a song goes but just can't remember its name, and boom: this app is for you. Of course, you could also turn it into a game – who can guess the song first? It's down to you.

To make things more exciting, we'll let users choose which music genres they specialize in, and we'll deliver them a push message whenever a new whistle comes in for that genre. They can then swipe to unlock and launch the app, and start posting suggestions for the song name.

CloudKit is something that was announced back in 2014, but it had some remarkably low limits that made it so unappealing I decided not to write a tutorial about it. Back then, each app got just 25MB per day of data transfer, growing at 0.5MB – yes, half a megabyte – per user per day, which was absurdly low. Since then Apple announced they were raising the limits so that every app now gets 2GB per month plus a further 50MB per user per month. Even better, database transfer is now judged on requests per second rather than an arbitrary data cap.

If you're not aware of what CloudKit does, I'll be going into much more detail later. For now, the least you need to know is that CloudKit lets you send and retrieve remote data for your app, effectively providing a server back-end for your app to talk to. Even better, unless you go over Apple's new generous limits, it's completely free.

Important warning: this project requires an active Apple developer program account, because CloudKit requires iCloud developer access. Although everything except push messaging works great in the simulator, you'll find CloudKit responds much faster on devices so you might find it easier to work from a device the entire time.

Please go ahead and create a new project in Xcode, choosing the Single View App template. Name it Project 33, then choose Swift for your language. Now strap yourself in, because this is going to be awesome…

SPONSOR Meet the new Instabug – more than just bug reporting! We help you build better apps and minimize your debugging time. With each bug report, we automatically capture details like network requests, repro steps, and session details. Get real-time crash reports with stack trace details and session data to help you catch and fix issues easily. And with our customizable in-app surveys, you’ll gather insightful user feedback and much more. Instabug is the fastest and easiest way to release with confidence. Start your free trial now! Start your free trial now!

< Overview   Next: Recording from the microphone with AVAudioRecorder >
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Average rating: 5.0/5

Click here to visit the Hacking with Swift store >>