How to read write files to iCloud Drive

Khoa Pham
2 min readJul 30, 2023

First, you need to enable iCloud Documents capability. Go to target settings -> Signing & Capabilities -> iCloud
`

Then inside your Info.plist, add this with your iCloud identifier and app name

<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.onmyway133.PastePal</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>PastePal</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>

To access to your iCloud Drive folder, we use FileManager to retrieve the folder.

Returns the URL for the iCloud container associated with the specified identifier and establishes access to that container.
If you specify nil for this parameter, this method returns the first container listed in the com.apple.developer.ubiquity-container-identifiers entitlement array.

Note that the ubiquity folder retrievement can take some time

Do not call this method from your app’s main thread. Because this method might take a nontrivial amount of time to set up iCloud and return the requested URL, you should always call it from a secondary thread. To determine if iCloud is available

--

--