【问题标题】:Swift write/save/move a document file to iCloud driveSwift 将文档文件写入/保存/移动到 iCloud 驱动器
【发布时间】:2015-11-24 19:54:47
【问题描述】:

我已经尝试了两天多将文件写入 iCloud 驱动器。我尝试直接在本地编写一个简单的文本文件,然后使用 UIDocumentMenuViewController 等移动它。我的代码没有任何错误并逐步通过调试器,它看起来很成功,但是当我检查文件是否存在或至少 iCloud 目录,那里什么都没有。我在模拟器和我的 iPhone 上都进行了尝试,触发了 iCloud 同步,以及我能想到的一切。

我的主要目标是简单地将文本文件写入 iCloud 驱动器,稍后将是“数字”文件

我已经设置了我的 plist 文件和我的权利:

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.paul.c.$(PRODUCT_NAME:rfc1034identifier)</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>myCloudTest</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

我还增加了捆绑版本,如:Save iOS 8 Documents to iCloud Drive

我已经尝试了几十个教程,但都没有成功。我的最新代码基于此示例:https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1

这是我的代码:

@IBAction func ExportFile(sender: AnyObject) {

    var error:NSError?

    let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("myCloudTest")

    //is iCloud working?
    if  iCloudDocumentsURL != nil {

        //Create the Directory if it doesn't exist
        if (!NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: nil)) {
                //This gets skipped after initial run saying directory exists, but still don't see it on iCloud
                NSFileManager.defaultManager().createDirectoryAtURL(iCloudDocumentsURL!, withIntermediateDirectories: true, attributes: nil, error: nil)
        }
    } else {
        println("iCloud is NOT working!")
        //  return
    }

    if ((error) != nil) {
        println("Error creating iCloud DIR")
    }


    //Set up directorys
    let localDocumentsURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: .UserDomainMask).last as! NSURL

    //Add txt file to my local folder
    let myTextString = NSString(string: "HELLO WORLD")
    let myLocalFile = localDocumentsURL.URLByAppendingPathComponent("myTextFile.txt")
    let written = myTextString.writeToURL(myLocalFile, atomically: true, encoding: NSUTF8StringEncoding, error: &error)

    if ((error) != nil){
        println("Error saving to local DIR")
    }


   //If file exists on iCloud remove it
    var isDir:ObjCBool = false
    if (NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: &isDir)) {
        NSFileManager.defaultManager().removeItemAtURL(iCloudDocumentsURL!, error: &error)
    }

    //copy from my local to iCloud
    if (error == nil && !NSFileManager.defaultManager().copyItemAtURL(localDocumentsURL, toURL: iCloudDocumentsURL!, error: &error)) {
        println(error?.localizedDescription);
    }

感谢您抽出宝贵时间。

干杯, 保罗

我在上面的代码之后在我的 iphone 上运行了一些代码:

var error:NSError?
    let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil) //?.URLByAppendingPathComponent("myCloudTest")

    var fileManager: NSFileManager = NSFileManager()


    var fileList: NSArray = fileManager.contentsOfDirectoryAtURL(iCloudDocumentsURL!, includingPropertiesForKeys: nil, options: nil, error: &error)!
    var filesStr: NSMutableString = NSMutableString(string: "Files in iCloud folder \n")
    for s in fileList {

        println(s)
    }

它会打印出我的文本文件的路径: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~paul~c~myApp/MyTextFile.txt

我的文件在那里,我只是在 iCloud 驱动器上看不到它。

【问题讨论】:

  • 你让它工作了吗?我有同样的问题,在这里尝试了所有的建议都没有成功。如果您通过在此处回答您自己的问题解决了问题,请告诉我们
  • 想知道您是否有机会将此代码迁移到 swift 3?谢谢!

标签: swift icloud uidocument icloud-drive uidocumentpickervc


【解决方案1】:

我遇到了这个问题。我followed the advice here 发现我的Info.plist 密钥不正确。一旦我将它更改为iCloud.MY_BUNDLE_IDENTIFIER(即从Info.plist 中更高的CFBundleIdentifier 键复制字符串),它就开始工作了。

从您的密钥中删除 .com 可能会解决您的问题。

【讨论】:

    【解决方案2】:

    FWIW:

    我还发现捆绑 ID 中的项目名称很重要。

    我的项目包 ID 类似于以下内容:aaa-bbb-ccc-ddd

    我无法让 iCloud 正常工作。

    然后我将其重命名为:aaa-bbb.ccc-ddd

    它开始工作了。

    【讨论】:

      【解决方案3】:

      我相信我已经找到了一种方法,可以让所有内容恢复同步,而不必经常“碰撞”我的捆绑包编号。在键值存储/iCloud Documents/CloudKit 的“功能”区域内进行更改时,我已经尝试过多次,而且每次似乎都有效。

      1. 在 Mac 上退出 iCloud
      2. 在您的模拟器上退出 iCloud
      3. 在 Mac 上重新登录 iCloud
      4. 在您的模拟器上重新登录 iCloud
      5. 从 XCode 进行干净的构建 (Shift-Cmd-K)

      当您的应用程序正在写入您的 iCloud 文档目录时,这似乎会重置文件夹结构的同步 - 而无需触摸您的捆绑包编号。这样做需要更长的时间,但我有点强迫症,有点喜欢我最初的应用程序启动以 1 开头!

      【讨论】:

        【解决方案4】:

        也许容器名称有一个规则。
        我尝试了以下名称。

        ○:iSheet
        ×:工作表文件
        ×:com.myname.sheetFiles

        【讨论】:

          猜你喜欢
          • 2022-10-26
          • 2021-03-18
          • 2020-02-07
          • 2015-01-08
          • 2019-01-02
          • 2014-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多