Learn how to sync files and data through a shared iCloud drive folder using the latest version of Swift programming language.
TL;DR: download the final source for the iCloud drive Swift tutorial
iCloud drive project setup tutorial
Let's start by creating a new project for iOS. You can select the single view application template, don't worry too much about document based apps, because in this tutorial we're not going to touch the UIDocument class at all. 🤷♂️
The first step is to enable iCloud capabilities, which will generate a new entitlements file for you. Also you'll have to enable the iCloud application service for the app id on the Apple developer portal. You should also assing the iCloud container that's going to be used to store data. Just a few clicks, but you have to do this manually. 💩
So I believe that now you have a proper iOS app identifier with iCloud capabilities and application services enabled. One last step is ahead, you have to add these few lines to your Info.plist file in order to define the iCloud drive container (folder name) that you're going to use. Note that you can have multiple containers for one app.
Finally we're ready to move forward with some actial coding. 💻
Files inside iCloud drive containers
Working with iCloud files using Swift is relatively easy. Basically you just have to get the base url of your iCloud drive container, and you can do whatever you want. 🤔
However I'll show you some best practices & tricks.
First you have to check if your container folder already exists, if not you should create it by hand using the FileManager class. I've also made a "shortcut" variable for the container base url, so I don't have to write all those long words again. 😅
Working with paths inside the iCloud drive container is simple, you can append path components to the base url and use that exact location url as you want.
Picking existing files is also quite straightforward. You can use the built-in document picker class from UIKit. There are only two catches here. 🤦♂️
First one is that you need to provide the type of the documents that you'd like to access. Have you ever heard about UTI's? No? Maybe yes...? The thing is that you have to find the proper uniform type identifier for every file type, instead of providing an extension or mime-type or something commonly used thing. Smart one, huh? 🧠
The second catch is that you have to "unlock" the picked file before you start reading it. That can be achieved by calling the startAccessingSecurityScopedResource method. Since Thanos told us that all thing should be perfectly balanced, please don't forget to call the stopAccessingSecurityScopedResource method, or you'll be evaporated. 🧤💧
Everything else works as you'd expect. You can save files directly into the container through file apis or by using the UIDocumentPickerViewController instance. Here are some of the most common api calls, that you can use to manipulate files.
You can read and write any kind of string, data. By using the FileManager you can copy, move, delete items or change file attributes. All your documents stored inside iCloud drive will be magically available on every device. Obviously you have to be logged in with your iCloud account, and have enough free storage. 💰
If you alter something in your settings you might want to increment your build number as well in order to notify the operating system about the changes. 💡
On the mac all the iCloud drive files / containers are located under the user's Library folder inside the Mobile Documents directory. You can simply use the Terminal or Finder to go there and list all the files. Pro tip: look for hidden ones as well! 😉
You can also monitor the activity of the CloudDocs daemon, by using this command:
The output will tell you what's actually happening during the sync.
I encourage you to check the manual entry for the brctl command, because there are a few more flags that can make troubleshooting more easy. 🤐
This article was heavily inspired by Marcin Krzyzanowski's really old blog post: