Restore your application’s data from iCloud and track progress

Rajat Sharma
The Startup
Published in
4 min readDec 10, 2019

In this article, I am going to guide you through a step by step process of restoring your application’s data backed up on iCloud and tracking the download progress on the way. Please note that this article is an extension of a previously posted article where I have gone through the process of backing up your application’s data on iCloud. If you have not read through the first part of this series, please stop right here and go through the first part of the article here.

What will you achieve by the end of this tutorial?

  • Download the application’s data already backed up on iCloud.
  • Track the progress of the backup as it is downloaded from iCloud.
  • Find the similarities in using NSMetadataQuery to upload and download the files on iCloud.

Prerequisites for implementing the iCloud restore.

  • Two real iOS device. I am going to use an iPhone X with iOS13 and an iPhone 7 Plus with iOS 13, but the actual minimum requirement is any real iOS device. This may work in an iOS simulator too but I have not tested in on a simulator at this point.
  • An apple ID logged in to your iOS device, with enough space available for the backup of the application.
  • A previous read of the first part of this article where we go through the process of uploading the data to iCloud.

Recap of uploading data to iCloud.

In the previous article, we created a new Xcode Project, named iCloud Backup. In our Xcode project, we created a Backup.swift file in which we initialised our NSMetadataQuery object and started the upload operation on this query.

Then, we added some notification observers and tracked the progress of the upload to iCloud using these observers. Finally, we added a new button on the ViewController.swift to start the upload of the file on iCloud. The progress can be seen in the console window on Xcode and after the upload is complete, the file can be viewed in the iCloud directory on the iPhone.

Now, let’s move ahead and implement the steps to download this file from the iCloud directory.

Step-wise implementation.

Now, that we already have our Xcode project setup with the uploading of our file to iCloud working, we are going to jump straight ahead to the implementation of downloading file from iCloud.

Step 1, adding a restore file: Let’s create a new file in our Xcode project called Restore.swift and add an instance of NSMetadataQuery and an initialisation method in it. The query instance is used to interact with the iCloud drive and start the download operation on it.

Next, let’s initialise our query by adding a new method initialiseQuery() and calling it from our viewDidLoad() method. This will initialise our query and provide a predicate to search for our sample.mp4 file on the iCloud drive.

Step 2, adding notification observers: Now, we are going to add notification observers to listen for the updates returned by our query. Specifically, we will listen to NSMetadataQueryDidStartGathering, which will let us know when the query starts gathering information and NSMetadataQueryGatheringProgress, giving us the progress of the gathering and NSMetadataQueryDidUpdate, that’ll be called each time an update about the operation is available.

Now that we have added our observers, let’s add a function to process the information. Let us add a function called processCloudFiles() where we are going to process the NSMetadataQuery updates.

Let’s go through the function above step by step. We are iterating the query.results and fetching the NSMetadataItem and URL for the file uploaded on iCloud. Once we have our fileURL, we are going to start the download from iCloud.

Next, to track the progress of the download, we are checking for two keys on the fileItem, which is an instance of our NSMetadataItem. These keys, NSMetadataUbiquitousItemDownloadingStatusKey and NSMetadataUbiquitousItemDownloadingStatusCurrent gives us the state of the downloading and the state of the downloaded file (if any) in our local iCloud directory.

To get the progress of downloading, we call NSMetadataUbiquitousItemPercentDownloadedKey on our fileItem, which returns us the progress as a Double value.

Step 3, adding a method to start download: Now, finally add a getBackup() method in our Restore.swift file, in which we will write the code to start the download process.

That is all for us to start downloading of our iCloud file and displaying its progress. Now, let’s test it out.

Step 4, adding a button to start the download: Let us now move to our ViewController.swift file and add a new button to start the download of the iCloud file. Add the following code in your viewDidLoad() method just below the code where the uploadButton is added.

Now, let’s add the action for this button. Add the following method in your ViewController.swift file.

If you followed all the steps correctly, your ViewController.swift file should appear as follows:

All right then! Everything looks good so far, it’s time to now test out the code that we have written and see it in action.

Once we run the code and take a backup as described in the previous article, we could verify the file was indeed uploaded to the iCloud drive. Now, we are going to switch to our other phone, login into the same iCloud account with which we took the backup and start downloading the file. You’ll see with some progress the file is downloaded on the new device and is visible in its iCloud directory.

Conclusion

Backing up our data to iCloud and restoring from iCloud is really not that difficult if we follow the appropriate steps. I hope I was able to help you with the process, let me know if you have any questions. Cheers!

--

--

Rajat Sharma
The Startup

Blockchain Solutions Architect | Mobile App Architect | Blockchain Consultant | R3 Corda | Ethereum | Hyperledger | Ripple | IOTA