0

I have successfully saved and loaded documents to and from the user's iCloud Drive using iCloud documents, but the document is visible in the user's iCloud Drive, like this:

iCloud Documents Screen App's Document

I do not want this, as this is saved data from my app. Is there any way to keep these documents hidden?

Here is my code:

var containerUrl: URL? {
    return FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
}

func read() -> String {
    let documentUrl = self.containerUrl!.appendingPathComponent("test.txt")
    print("url: \(documentUrl)")
    
    do {
        return try String(contentsOf: documentUrl)
    }
    catch {
        alert(text: error.localizedDescription)
        return "Error"
    }
}


func save(text:String) {
    if let url = self.containerUrl, !FileManager.default.fileExists(atPath: url.path, isDirectory: nil) {
        do {
            try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
        }
        catch {
            alert(text: error.localizedDescription)
        }
    }
    
    let documentUrl = self.containerUrl!.appendingPathComponent("test.txt")
    print("url: \(documentUrl)")
    
    do {
        try text.write(to: documentUrl, atomically: true, encoding: .utf8)
    }
    catch {
        alert(text: error.localizedDescription)
    }
}

and what I added to my Info.plist:

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.testCloudStorage</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>testCloudStorage</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

Any help would be appreciated.

JustMakeStuff
  • 419
  • 3
  • 19

1 Answers1

1

If you don't want to expose this data to the user what you need is to save it to the library directory. Please take some time and read File System Basics.

Use the Library subdirectories for any files you don’t want exposed to the user.

The contents of the Library directory (with the exception of the Caches subdirectory) are backed up by iTunes and iCloud.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • One more question: when I save a document to iCloud in this way, it takes hours for it to be able to be read on my other device. Is this unavoidable, or do I have to force iCloud refresh or something? – JustMakeStuff Apr 15 '22 at 15:45
  • @JustMakeStuff I don't know. There is no info about how often the data is synced. How much data are you talking about? – Leo Dabus Apr 15 '22 at 15:54
  • @JustMakeStuff From the docs The iCloud File Storage Container **Anything that the app does not want the user to see or modify directly should be placed outside of the Documents directory. Apps can create any subdirectories inside the container directory, so they can arrange private files as desired.** – Leo Dabus Apr 15 '22 at 15:57
  • @JustMakeStuff this might help https://developer.apple.com/forums/thread/69716 – Leo Dabus Apr 15 '22 at 17:10
  • Thanks for all the help. I am saving a lot of data, however the tests I did which took a long time were with a tiny amount of data. However, it only takes a long time when I create the file, not when I save and load data to and from it, which takes only a few seconds. – JustMakeStuff Apr 16 '22 at 00:03