You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将文件保存至iCloud Drive并在文件App中显示问题求助

问题:iCloud Drive文件无法在“文件”App中显示

已严格按照相关步骤操作,仍无法将文件保存至iCloud Drive并在“文件”App中显示。具体信息如下:

代码实现

func save() {
    do {
        if let containerUrl = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") {
            if !FileManager.default.fileExists(atPath: containerUrl.path, isDirectory: nil) {
                do {
                    try FileManager.default.createDirectory(at: containerUrl, withIntermediateDirectories: true, attributes: nil)
                } catch {
                    print(error.localizedDescription)
                }
            }                   
            let fileURL = containerUrl.appendingPathComponent("test2.txt")
            
            if FileManager.default.fileExists(atPath: fileURL.path, isDirectory: nil) {
                try FileManager.default.removeItem(at: fileURL)
            }
            try data.write(to: fileURL)

            print("Drawing saved to \(fileURL)")
        }
    } catch {
        print("Error saving drawing: \(error)")
    }
}

Info.plist配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>UISupportsDocumentBrowser</key>
    <true/>
    <key>NSUbiquitousContainers</key>
    <dict>
        <key>iCloud.com.mycompany.TestSketchSave</key>
        <dict>
            <key>NSUbiquitousContainerIsDocumentScopePublic</key>
            <true/>
            <key>NSUbiquitousContainerName</key>
            <string>TestSketchSave</string>
            <key>NSUbiquitousContainerSupportedFolderLevels</key>
            <string>Any</string>
        </dict>
    </dict>
</dict>
</plist>

额外信息

  • iCloud容器ID:iCloud.com.mycompany.TestSketchSave
  • App ID:com.mycompany.TestSketchSave
  • 文件已在iCloud Drive设置中显示,且已升级App版本号,但“文件”App中仍无该文件。

排查与解决建议

  • 指定明确的容器ID:代码中url(forUbiquityContainerIdentifier: nil)会默认取第一个容器,为避免歧义,直接传入你的容器ID:
    if let containerUrl = FileManager.default.url(forUbiquityContainerIdentifier: "iCloud.com.mycompany.TestSketchSave")?.appendingPathComponent("Documents") {
        // 后续逻辑不变
    }
    
  • 确认文件同步状态:iCloud同步存在延迟,可通过以下代码检查文件是否标记为同步项:
    let isUbiquitous = FileManager.default.isUbiquitousItem(at: fileURL)
    print("Is file ubiquitous: \(isUbiquitous)")
    
    若未标记,手动将文件标记为iCloud同步项:
    try FileManager.default.setUbiquitous(true, itemAt: fileURL, destinationURL: fileURL)
    
  • 验证写入路径正确性:打印containerUrl的完整路径,确认是否指向iCloud容器的Documents目录(路径通常类似/private/var/mobile/Library/Mobile Documents/iCloud~com~mycompany~TestSketchSave/Documents/)。
  • 添加文件类型标识:为文件设置UTI类型,确保“文件”App能识别:
    import MobileCoreServices
    
    // 写入文件后添加
    var resourceValues = URLResourceValues()
    resourceValues.typeIdentifier = kUTTypePlainText as String
    try fileURL.setResourceValues(resourceValues)
    
  • 重置iCloud同步:前往设置 → [你的账号] → iCloud → iCloud Drive → 关闭并重新开启该App的同步权限,等待同步完成。
  • 重启设备与App:清除App缓存,重启设备后再次测试保存逻辑。

内容的提问来源于stack exchange,提问作者erotsppa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 18:12:47