iosswifticloudicloud-documents

Swift - Can't connect to iCloud documents


I am trying to use iCloud documents to store backup files from an iOS app (Xcode 11.3 and Swift 5).

I set up the base of the iCloud capability but I can't get an URL for the container.

Here is the iCloud capability setup enter image description here

The entitlements file enter image description here

The key added in the Info.plist

<key>NSUbiquitousContainers</key>
    <dict>
        <key>iCloud.com.alienfamily.TestiCloudAgain</key>
        <dict>
            <key>NSUbiquitousContainerIsDocumentScopePublic</key>
            <true/>
            <key>NSUbiquitousContainerName</key>
            <string>myCloudTest</string>
            <key>NSUbiquitousContainerSupportedFolderLevels</key>
            <string>Any</string>
        </dict>
    </dict>

The code I tried

let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")

if iCloudDocumentsURL != nil {
    print("All good")
} else {
    print("I'm sad")
}

I follow many tutorials (like this one tutorial ) and check other posts (stackoverflow) without any results.

Does anyone know what I am doing wrong?


Solution

  • My solution was pretty simple, it was to test it on my iPhone who have an account correctly setup and not on the simulator.