我在应用程序中使用iCloud Drive时遇到了困难。我无法在iCloud Drive中创建应用程序的文件夹。
昨天,我设法创建了一个文件夹并在其中保存了项目,但是今天,在运行代码之后,该文件夹消失了,无法保存文件了。
这是我昨天成功运行并投入使用的示例代码:
let str = "ivan cantarino"
guard let iCloudPath = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") else {
print("Failed to get iCloud path")
return
}
print("icloud path:",iCloudPath.absoluteString)
// prints: icloud path: file:///private/var/mobile/library/Mobile%20Documents/iCloud~com~ivancantarino~Cozynote/Documents
let lastPath = iCloudPath.appendingPathComponent("iCloudDrive.txt")
// writes a file to the directory
do {
try str.write(to: lastPath,atomically: true,encoding: .utf8)
print("success")
} catch {
print("failed to write with error:",error.localizedDescription)
// prints: failed to write with error: The file “iCloudDrive.txt” couldn’t be saved in the folder “Documents”.
}
这是我的info.plist iCloud信息:
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.ivancantarino.Cozynote</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>Cozynote</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
这是一张印刷品,表明我使用的是正确的容器:
我碰到了该应用程序的版本和内部版本号,重新安装了该应用程序,但是以某种方式我无法写入该路径。 它一直在打印错误。
有人可以在这里发现我做错了什么吗?