MarcWiggerman
MarcWiggerman

Reputation: 101

Adding iCloud Drive File Manager to App - iOS 13

I want to support adding files to iCloud within my iOS Application. I want these files to be added to a folder on iCloud Drive (e.g. like the Numbers or Pages folders). I tried everything I found online about this subject but I did not manage to add a folder to iCloud Drive.

First, I added NSUbiquitousContainers to the info.plist

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.<xyz>.AppName</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>App Name</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

After this, I added the iCloud capability to my app: iCloud capability

Then, I added the following code to my ViewController:

if let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") {
    if (!FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil)) {
        do {
            try FileManager.default.createDirectory(at: iCloudDocumentsURL, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error.localizedDescription)
        }
    }
}

Note: after setting breakpoints I discovered that the code after if (!FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil)) { never gets called. I temporarily removed this line, but this did not do anything.

Finaly, I added a file using the following code:

//Save text file to local directory
let file = "file.txt" //Text file to save to iCloud
let text = "sample text" //Text to write into the file
func saveToDirectory(){
    guard let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
    let fileURL = directory.appendingPathComponent(file)

    print(directory)
    //Writing
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    } catch {
        /* error handling here */
        print("Error in writing")
    }

    //Reading
    do {
        let getText = try String(contentsOf: fileURL, encoding: .utf8)
        print(getText)
    } catch {
        /* error handling here */
        print("Error in reading")
    }
}

I do not see the app's folder, or the above file appear on my iCloud Drive. But when I download the apps container and look at it's contents I see the file listed under /AppData/Documents.

Did someone else got this? And/or does someone know how to solve this?

Thanks!

I also changed the Build/Version numbers of my app, but without any result

Upvotes: 3

Views: 1002

Answers (1)

jki
jki

Reputation: 4791

You should copy your file to iCloud container, you are writing it to app sandbox only.

First, create Documents folder on your iCloud container - if you store file in other folder, your app on another device will see the folder but it won't appear in Files app.

Then copy your file.txt into Documents folder on your iCloud container.

Upvotes: 0

Related Questions