【问题标题】:SetUbiquitous showing 'file already exists' for file that doesn'tSetUbiquitous 为不存在的文件显示“文件已存在”
【发布时间】:2016-10-07 18:20:26
【问题描述】:

AppDelegate.swift 中,首次启动时,目的是将一些示例文档放置在本地 Documents 文件夹中,如果启用了 iCloud,则放置在 iCloud Documents 文件夹中。

    var templates = NSBundle.mainBundle().pathsForResourcesOfType(AppDelegate.myExtension, inDirectory: "Templates")
    dispatch_async(appDelegateQueue) {
        self.ubiquityURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)
        if self.ubiquityURL != nil && templates.count != 0 {
            // Move sample documents from Templates to iCloud directory on initial launch
            for template in templates {
                let tempurl = NSURL(fileURLWithPath: template)
                let title = tempurl.URLByDeletingPathExtension?.lastPathComponent

                    let ubiquitousDestinationURL = self.ubiquityURL?.URLByAppendingPathComponent(title!).URLByAppendingPathExtension(AppDelegate.myExtension)
                    // let exists = NSFileManager().isUbiquitousItemAtURL(ubiquitousDestinationURL!)
                    do {
                        try NSFileManager.defaultManager().setUbiquitous(true, itemAtURL: tempurl, destinationURL: ubiquitousDestinationURL!)
                    }
                    catch let error as NSError {
                        print("Failed to move file \(title!) to iCloud: \(error)")
                    }
            }
        }
        return
    }

在运行此之前,我从设备中删除了该应用程序,并确保 iCloud 中没有该名称的文档。首次启动时,没有 iCloud,示例文档会正确复制到本地 Documents 文件夹中。使用 iCloud,此代码会运行,setUbiquitous 调用会导致错误,指出文件已存在。对isUbiquitousItemAtURL 的注释调用也返回true。

是什么让这些调用注册了一个我很确定不存在的文件?谢谢!

【问题讨论】:

    标签: ios swift icloud appdelegate


    【解决方案1】:

    文件已经存在,直接替换

    【讨论】:

    • 没有其他证据表明它们存在。显示云文件的元查询没有找到它们,但会找到所有其他文档。无法通过 Mac 的文件系统(iCloud 文件夹)找到它们。无法使用 iMazing 找到它们。我确实知道它正在寻找导致它产生该错误的东西,但我无法弄清楚是什么或在哪里。
    【解决方案2】:

    主要解决方案...在所有的试验和错误中,我忘记将“文档”放回 url。应该是:

    let ubiquitousDestinationURL = self.ubiquityURL?.URLByAppendingPathComponent("Documents").URLByAppendingPathComponent(title!).URLByAppendingPathExtension(AppDelegate.myExtension)
    

    否则,将文件写入错误的目录,因此我无法正常查看。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 2016-09-07
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      相关资源
      最近更新 更多