iCloud Document Storage

Parth Gohel
Mindful Engineering
3 min readJun 24, 2021

iCloud Drive is Apple’s essential connection between all your devices, Mac, iPhone, iPad, even your Windows PC.While the cost of storage seems expensive in comparison to other online storage services, its advantage is that it works natively across all your devices.

In this iCloud tutorial, you’ll learn how to create folder and add files in iCloud from your app as well as how to move and copy files from Local to iCloud and Vice versa.

You can download source code at end of this blog.

Let’s see how to do it 🙂

Setting up iCloud Storage APIs

Let’s start by creating a new project for iOS. You can select the single view application template.
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 in your project. Also you’ll have to enable the iCloud application service for the app id on the Apple developer portal. You should also assign the iCloud container that’s going to be used to store data. you have to do this manually.

Note: You need a valid Apple Developer Program membership in order to set advanced app capabilities like iCloud support. So you have to pay $99/year🤑

The next step is to modify the Info.plist. I would recommend selecting "Open As" option and use "Source Code" so you can work with pretty standard XML.

Finally we’re ready to move forward with some actual coding. 💻

Upload files to iCloud

Upload files to iCloud is relatively easy basically you just have to get the base url of your iCloud drive container, and you can do whatever you want.

We can get the local and iCloud container URL like this:

We don’t even need to pass the identifier, with nil we get the default (and only one) container.

Note: The appendingPathComponent("Documents") is very important bit and without it your folder will not show inside iCloud Drive.

Let’s create folder in iCloud.

You can create folder by using below code 👇

First you have to check iCloud enabled or not and then we check folder already exist or not if not you should create it by hand using the FileManager class.

Cool! We successfully created folder in iCloud container.

Let’s create file in this directory.

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.

You can create file in existing folder by using below code 👇

First you have to check folder exist or not if it’s exist we try to create text file in that directory by appending file name with its extension.

Move and Copy file from Local to iCloud and Vice versa

You can read and write any kind of string, data by using the FileManager you can copy, move, delete items or change file attributes.

You can move/copy local file to iCloud by using above code if you want to move/copy file from iCloud to local just switch your url in above code.

Note: When you try to send the file to iCloud you should set the flag to true. you where using false which is to remove the file from iCloud.

Conclusion

When I first started working with iCloud Drive I could not find many resources and it is possible I left out something important or useful. If that is the case, please let me know in the comments.👇

I hope you like this tutorial.🙂
Thanks for reading!

--

--