【问题标题】:App not recreating iCloud Drive folder应用程序未重新创建 iCloud Drive 文件夹
【发布时间】:2017-07-03 00:05:15
【问题描述】:

我正在构建一个应用程序,它将其文件保存到用户的 iCloud Drive 文件夹中。一切都很好,直到我删除了 iCloud Drive 中的应用程序文件夹,看看会发生什么。从那时起,每次我打开应用程序时,它都会尝试将我的示例文件保存在 iCloud 中,但它不起作用(reloadiCloud() 函数设置为在 ViewWillAppear 中运行)。我还需要做些什么来创建 iCloud Drive 文件夹吗?我的代码如下。

func reloadiCloud() {
    ubiquityURL = filemgr.url(forUbiquityContainerIdentifier: nil)

    guard ubiquityURL != nil else {
        print("Unable to access iCloud Account")
        return
    }

    ubiquityURL = ubiquityURL?.appendingPathComponent("Documents")

    metaDataQuery = NSMetadataQuery()

    metaDataQuery?.predicate = NSPredicate(format: "NOT %K.pathExtension = ''", NSMetadataItemFSNameKey)
    metaDataQuery?.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.metadataQueryDidFinishGathering), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: metaDataQuery!)

    metaDataQuery!.start()
}

func metadataQueryDidFinishGathering(notification: NSNotification) -> Void {
    let query: NSMetadataQuery = notification.object as! NSMetadataQuery

    query.disableUpdates()

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query)

    query.stop()

    if query.resultCount > 0 {
        fileList = []
        for var i in (0..<query.resultCount) {
            let currentFileURL = query.value(ofAttribute: NSMetadataItemURLKey, forResultAt: i) as! URL
            fileList.append(currentFileURL.lastPathComponent)
            tableView.reloadData()
        }
        tableView.reloadData()
        print(fileList)
    } else {
        let sampleFile = Bundle.main.url(forResource: "Sample", withExtension: "pdf")!
        movetoiCloud(forURL: sampleFile)
    }
}



func movetoiCloud(forURL: URL) {
    let fileName = forURL.lastPathComponent
    ubiquityURL = filemgr.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents/\(fileName)")

    //Check if the filename exists and add "copy" to the new version
    for var i in (0..<fileList.count) {
        if fileList[i] == forURL.lastPathComponent {
            let base = ubiquityURL?.absoluteURL.deletingLastPathComponent()
            let endIndex = fileName.index(fileName.endIndex, offsetBy: -4)

            let truncatedExtension = fileName.substring(from: endIndex)
            let truncatedName = fileName.substring(to: endIndex).appending(" copy").appending(truncatedExtension)

            ubiquityURL = base?.appendingPathComponent(truncatedName)
            print("New ubiquity URL: \(ubiquityURL)")
        }
    }

    do {
        try filemgr.setUbiquitous(true, itemAt: forURL, destinationURL: (ubiquityURL)!)
        reloadiCloud()
        print("Move to iCloud OK")
    } catch let error {
        print("moveToiCloud failed: \(error.localizedDescription)")
    }
}

【问题讨论】:

    标签: ios swift icloud icloud-drive


    【解决方案1】:

    尝试更新 info plist 中的捆绑包编号和版本,需要增加这些值才能使 icloud 添加生效。

    【讨论】:

    • Bumping Bundle 版本立即为我工作。谢谢!
    • 乐于助人! @damote
    • 天啊,我搜索了 2 天。但是如果用户稍后从驱动器中删除文件夹,它会重新创建文件夹吗?
    • 还有一个问题,为什么文件夹不像其他应用一样显示我的应用图标?
    • 对于第一个问题,您可以检查该文件夹是否存在于您的代码中。如果不创建一个新的,则将其用作您的目标文件夹。让我看看能不能找到它的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2016-01-07
    • 2012-03-02
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多