Swift将文档文件写入/保存/移动到iCloud驱动器

问题描述 投票:8回答:3

我已经尝试了两天多来写一个文件到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 icloud uidocument icloud-drive uidocumentpickervc
3个回答
3
投票

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

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


3
投票

FWIW:

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

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

我无法让iCloud工作。

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

它开始工作了。


0
投票

我相信我找到了一种让所有东西重新同步的方法,而不必经常“碰撞”我的捆绑号码。我在键值存储/ iCloud Documents / CloudKit的“功能”区域内进行了多次尝试,并且每次都可以使用。

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

当您正在将应用程序写入iCloud Documents目录时,这似乎会重置文件夹结构的同步 - 而无需触及您的软件包编号。这需要一点时间,但我是一个小强迫症,有点喜欢我的初始应用程序启动以1开始!

© www.soinside.com 2019 - 2024. All rights reserved.