Hide iCloud Documents

858 views Asked by At

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.

1

There are 1 answers

5
Leo Dabus On BEST ANSWER

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.