简体   繁体   English

如何将文件备份到 iCloud 但未显示在 iCloud Drive 中?

[英]How to backup file to iCloud but not shown in iCloud Drive?

I'm backup my database realm file to iCloud by using FileManage methods.我正在使用FileManage方法将我的数据库领域文件备份到 iCloud。 Everything works fine, but there's a trick I want to achieve is:一切正常,但我想要实现的一个技巧是:

The file should be backup by iCloud, but NOT shown in iCloud Drive.

For example, GoodNotes 5, we can open the iCloud in settings, verify it's truly backup in iCloud Storage, and the GoodNots 5 App File is not shown in iCloud Drive.比如GoodNotes 5,我们可以在设置中打开iCloud,在iCloud Storage中验证它是真的备份,而iCloud Drive中没有显示GoodNots 5 App File。 在此处输入图像描述

Below is my code that implement iCloud backup, but the App File ama will be shown in iCloud Drive:下面是我实现 iCloud 备份的代码,但 App File ama将显示在 iCloud Drive 中:

private func retrieveLocalRealmURL() -> URL {
    let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentaryDirectory = urls[0]
    let realmURL = documentaryDirectory.appendingPathComponent("ama.realm");
    
    return realmURL
}

private func backupRealmToiCloudDrive() {
    let backgroundQueue = DispatchQueue.global(qos: .background)
    
    backgroundQueue.async {
        guard
            let ubiquityURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)
        else {
            return
        }
            
        let iCloudDriveURL = ubiquityURL.appendingPathComponent("Documents")
        let iCloudRealmURL = iCloudDriveURL.appendingPathComponent("ama.realm")
        
        let fileExists = FileManager.default.fileExists(atPath: iCloudDriveURL.path, isDirectory: nil)
        
        func copy() {
            let localRealmURL = self.retrieveLocalRealmURL()
            
            do {
                try FileManager.default.copyItem(at: localRealmURL, to: iCloudRealmURL)
            } catch {
                printLog(error.localizedDescription)
            }
        }
        
        if fileExists {
            self.deleteExistedFile(iCloudRealmURL)
            copy()
        } else {
            do {
                try FileManager.default.createDirectory(at: iCloudDriveURL, withIntermediateDirectories: true, attributes: nil)
                copy()
            } catch {
                printLog(error.localizedDescription)
            }
        }
    }
}

private func deleteExistedFile(_ url: URL) {
    let fileCoordinator = NSFileCoordinator(filePresenter: nil)
    
    fileCoordinator.coordinate(writingItemAt: url, options: .forDeleting, error: nil) { deleteURL in
        do {
            let fileExists = FileManager.default.fileExists(atPath: deleteURL.path, isDirectory: nil)
            
            if fileExists {
                try FileManager.default.removeItem(at: deleteURL)
            }
        } catch {
            printLog(error.localizedDescription)
        }
    }
}

After many tests, to avoid backup file to iCloud Drive, but in iCloud container, you can just simply remove the 'Documents' path, but using file path directly, like below:经过多次测试,为避免将文件备份到 iCloud Drive,但在 iCloud 容器中,您可以简单地删除“文档”路径,而是直接使用文件路径,如下所示:

let ubiquityURL = FileManager.default.url(forUbiquityContainerIdentifier: "identifier.abcdefg")

let iCloudDriveURL = ubiquityURL.appendingPathComponent("Documents") //remove this line
let iCloudRealmURL = iCloudDriveURL.appendingPathComponent("ama.realm")

change to:改成:

let iCloudRealmURL = ubiquityURL.appendingPathComponent("ama.realm") //appending to ubiquityURL directly

In this case, the backup file will be stored in iCloud container, but not shown in the user's iCloud Drive.在这种情况下,备份文件将存储在 iCloud 容器中,但不会显示在用户的 iCloud Drive 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM