隐藏 iCloud 文档

Hide iCloud Documents

我已经使用 iCloud 文档成功地在用户的 iCloud Drive 中保存和加载文档,但是文档在用户的 iCloud Drive 中是可见的,如下所示:

我不想要这个,因为这是从我的应用程序中保存的数据。有什么方法可以隐藏这些文件吗?

这是我的代码:

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)
    }
}

以及我添加到 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>

如有任何帮助,我们将不胜感激。

如果您不想将此数据公开给用户,您需要将其保存到库目录中。请花点时间阅读 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.