3

I'll let the user save backup of his data using iCloud drive. That works good. But if I switch to another iCloud Drive enabled device, I can't read the file.

I can see the backup file within the iCloud Drive App, but it's not downloaded, (it shows the "download from icloud symbol).

Is it possible to force the download of all files available within the directory if the user wants to restore a backup?

Using Swift 2 on iOS 8/9.

Christian
  • 6,961
  • 10
  • 54
  • 82

1 Answers1

3

I've solved this by myself. I'll check whether my backup file is available and if it needs to be downloaded. If so, I'll start the download and inform the user to check back in a few seconds. Not the best solution, but for now it works.

func checkAndDownloadBackupFile() -> Bool{
    if(iCloudDocumentsURL != nil){
        let file = iCloudDocumentsURL!.URLByAppendingPathComponent("backup.file")
        let filemanager = NSFileManager.defaultManager();

        if !filemanager.fileExistsAtPath(file.path!){

            if filemanager.isUbiquitousItemAtURL(file) {
                let alertView:UIAlertView = UIAlertView()
                alertView.title =  "Warning"
                alertView.message = "iCloud is currently busy syncing the backup files. Please try again in a few minutes."
                alertView.addButtonWithTitle("OK")
                alertView.show()

                do {
                    try filemanager.startDownloadingUbiquitousItemAtURL(file)
                } catch{
                    print("Error while loading Backup File \(error)")
                }
            }
            return false
        } else{
            return true
        }
    }
    return true
}
Christian
  • 6,961
  • 10
  • 54
  • 82
  • According to docs isUbiquitousItemAtURL is not the way to get downloaded status. "This method does not reflect whether the file has actually been uploaded to any iCloud servers." – Vito Valov Jun 19 '19 at 09:41
  • But it does seem to indicate if the file/folder is an iCloud file/folder. – Chris Prince Mar 23 '20 at 15:58
  • This technique is working for me-- for folders. In my case the folder names show up, but not their contents. Using `startDownloadingUbiquitousItem` on each iCloud Drive folder triggers the downloading. – Chris Prince Mar 23 '20 at 16:00
  • Are there any best practices how to deal with such situations? I try to force the startDownloadingUbiquitousItemAtURL at a specific folder in the iCloud, but this is not working for me. It only works if you know the filename.. – MosTwanTedT Jan 31 '21 at 18:23