【问题标题】:Access iCloud documents in iOS在 iOS 中访问 iCloud 文档
【发布时间】:2018-02-27 00:04:02
【问题描述】:

我正在尝试在 iOS 应用程序中访问已放入 Mac 上的 iCloud 文档文件夹中的文档,但我不知道如何操作。

let manager = FileManager.default  
let dir = manager.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
let files = try? FileManager.default.contentsOfDirectory(at: dir!, includingPropertiesForKeys: nil, options: [])
print(files) //(Optional([])

我在我的应用程序功能中启用了 iCloud Documents,并且我知道我桌面上的 Documents 文件夹中有文件..

我显然在这里遗漏了一些东西..

提前致谢

【问题讨论】:

  • 子类 UIDocument.
  • 感谢您的回复。一旦我有了 url,我就可以看到它对单个文档是如何工作的,但我什至无法从文档文件夹中获取 url 列表
  • 试试这个解决方案stackoverflow.com/a/50397424/2171764

标签: ios swift icloud-documents


【解决方案1】:
  1. 为 iCloud 创建 iCloud 容器

    一个。登录 developer.apple.com

    b.转到标识符并添加 iCloud 容器。

    c。 iCloud 容器一旦创建就无法删除。

  2. 创建包含 iCloud 功能的 App ID

    一个。创建应用 ID

    b.添加具有显式 Bundle ID 的 iCloud 支持。

    c。创建 Provisioning Profile 并将其下载到 Xcode。

  3. 在 Xcode 中配置功能

    一个。单击并从“+ Capability”中添加 iCloud 支持

    b.对于 iCloud 文档存储,选择 iCloud Documents 并选择您创建的容器。

    c。在 Info.plist 中添加 NSUbiquitousContainers 键

    d。里面的“MyApp”将显示为文件夹名称。

<key>NSUbiquitousContainers</key>
    <dict>
        <key>iCloud.com.example.MyApp</key>
        <dict>
            <key>NSUbiquitousContainerIsDocumentScopePublic</key>
            <true/>
            <key>NSUbiquitousContainerSupportedFolderLevels</key>
            <string>Any</string>
            <key>NSUbiquitousContainerName</key>
            <string>MyApp</string>
        </dict>
    </dict>
  1. 测试代码片段

    一个。试试下面的快速而肮脏的测试。

    let driveURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
    if let unwrappedFU = driveURL {
        print("iCloud available")
        let fileURL = driveURL!.appendingPathComponent("test.txt")
        try? "Hello word".data(using: .utf8)?.write(to: fileURL)
    } else {
        print("iCloud not available")
    }
  1. 写入您的应用文档文件夹不同于 iCloud 文档存储。您可以使您的 App Document 文件夹可见。通过添加到 Info.plist 2 附加属性。

    一个。添加支持打开文件到位:是

    b.支持就地打开文件:是的

    c。使用 Tim 的代码 sn-p 进行测试。 https://www.youtube.com/watch?v=p1UNnsodJxc


    let file = "\(UUID().uuidString).txt"
    let contents = "Some text..."
    
    let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let fileURL = dir.appendingPathComponent(file)
    
    do {
        try contents.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {
        print("Error: \(error)")
    }

注意:您会注意到“在我的 iPhone”文件夹中的文件应用程序中的应用程序文件夹。那是您向用户公开的 App 的 Document 文件夹。您还会注意到 iCloud Drive 文件夹中的应用程序文件夹。这两个文件夹是不同的。您应该使用您的 App 文件夹并使用 iCloud Drive 文件夹作为您的导出目的地,这样您就不必过多地处理访问控制。

CAVEAT:奇怪的是,这一切都不起作用,包括在 iPhone 上退出 iCloud。即使它在模拟器上工作。在我的情况下,它只有在 iPhone 重新启动时才开始工作。

【讨论】:

    【解决方案2】:
     func clickFunction(){
    
                let DOCUMENTS_DIRECTORY = "Documents"
                if let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent(DOCUMENTS_DIRECTORY){
                    if let pathComponent = iCloudDocumentsURL.path as? String {
                        if (FileManager.default.fileExists(atPath: pathComponent, isDirectory: nil)) {
    
                            //Write it out the code of getting files
                            let fileManager = FileManager.default
                            let enumerator = fileManager.enumerator(atPath: DocumentsDirectory.iCloudDocumentsURL!.path!)
                            while let file = enumerator?.nextObject() as? String {
                                //this this the file or document path.
                            }
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2016-02-16
      • 2015-05-31
      • 2013-09-02
      • 2012-05-09
      • 1970-01-01
      相关资源
      最近更新 更多