I have implemented the backup system like we have in WhatsApp.
- First, it is checking for the document directory and zip the data to temporary directory.
Then get the url to iCloud
func getUrlToiCloud() -> URL? { let fileManager = FileManager.default let url = fileManager.url(forUbiquityContainerIdentifier: nil) return url }
Next is to backup the items to iCloud
func iCloudBackupHandler(_ backupName: String, backupPath: String) { guard let url = getUrlToiCloud() else { return } let loggedInUser: String if #available(iOS 10.0, *) { loggedInUser = CKCurrentUserDefaultName } else { loggedInUser = CKOwnerDefaultName } print("loggedInUser:\(loggedInUser)") let localBackupUrl = URL(fileURLWithPath: backupPath) let iCloudDocumentUrl = url.appendingPathComponent(backupName) print("Local:\(localBackupUrl)") print("iCloudDocumentUrl:\(String(describing: iCloudDocumentUrl))") do { try FileManager.default.replaceItem(at: iCloudDocumentUrl, withItemAt: localBackupUrl, backupItemName: backupName, options: .withoutDeletingBackupItem, resultingItemURL: nil) //try FileManager.default.setUbiquitous(true, itemAt: localBackupUrl, destinationURL: iCloudDocumentUrl) } catch { print("error while backup: \(error)") } }
My question is here, on the following line. which backups the item to iCloud.
try FileManager.default.replaceItem(at: iCloudDocumentUrl, withItemAt: localBackupUrl, backupItemName: backupName, options: .withoutDeletingBackupItem, resultingItemURL: nil)
How can I track the progress of the file being uploaded to iCloud? As in WhatsApp there is a progress(percentage) of the file being backed up. Please let me know, if you have idea on this.