Categories
apple ios

Restore your application’s information from iCloud and monitor development

Rajat Sharma

In this newsletter, I’m going to lead you via a step-by-step technique of restoring your application’s information sponsored up on iCloud and monitoring the download development at the approach. Please be aware that this newsletter is an extension of a up to now posted article the place I’ve long past throughout the technique of backing up your application’s information on iCloud. If you haven’t learn throughout the first a part of this sequence, please forestall proper right here and cross throughout the first a part of the item right here.

  • Download the application’s information already sponsored up on iCloud.
  • Track the development of the backup as it’s downloaded from iCloud.
  • Find the similarities in the use of NSMetadataQuery to add and download the recordsdata on iCloud.
  • Two actual iOS tool. I’m going to make use of an iPhone X with iOS13 and an iPhone 7 Plus with iOS 13, however the true minimal requirement is any actual iOS tool. This might paintings in an iOS simulator too however I’ve now not examined in on a simulator at this level.
  • An apple ID logged in for your iOS tool, with enough room to be had for the backup of the application.
  • A prior learn of the first a part of this newsletter the place we cross throughout the technique of importing the information to iCloud.

In the former article, we created a brand new Xcode Project, named iCloud Backup. In our Xcode challenge, we created a Backup.swift record wherein we initialised our NSMetadataQuery object and began the add operation in this question.

Then, we added some notification observers and tracked the development of the add to iCloud the use of those observers. Finally, we added a brand new button at the ViewController.swift to begin the add of the record on iCloud. The development may also be noticed within the console window on Xcode and after the add is whole, the record may also be considered within the iCloud listing at the iPhone.

Now, let’s transfer forward and put in force the stairs to download this record from the iCloud listing.

Now, that we have already got our Xcode challenge setup with the importing of our record to iCloud operating, we’re going to soar immediately forward to the implementation of downloading record from iCloud.

Step 1, including a repair record: Let’s create a brand new record in our Xcode challenge referred to as Restore.swift and upload an example of NSMetadataQuery and an initialisation means in it. The question example is used to engage with the iCloud force and get started the download operation on it.

Next, let’s initialise our question via including a brand new means initialiseQuery() and calling it from our viewDidLoad() means. This will initialise our question and supply a predicate to seek for our pattern.mp4 record at the iCloud force.

Step 2, including notification observers: Now, we’re going to upload notification observers to pay attention for the updates returned via our question. Specifically, we will be able to pay attention to NSMetadataQueryDidStartGathering, which is able to tell us when the question begins collecting knowledge and NSMetadataQueryGatheringProgress, giving us the development of the collection and NSMetadataQueryDidUpdate, that’ll be referred to as every time an replace in regards to the operation is to be had.

Now that we have got added our observers, let’s upload a serve as to procedure the tips. Let us upload a serve as referred to as processCloudFiles() the place we’re going to procedure the NSMetadataQuery updates.

Let’s cross throughout the serve as above step-by-step. We are iterating the question.effects and fetching the NSMetadataItem and URL for the record uploaded on iCloud. Once now we have our fileURL, we’re going to get started the download from iCloud.

Next, to trace the development of the download, we’re checking for two keys at the fileItem, which is an example of our NSMetadataItem. These keys, NSMetadataUbiquitousItemDownloadingStatusKey and NSMetadataUbiquitousItemDownloadingStatusCurrent provides us the state of the downloading and the state of the downloaded record (if any) in our native iCloud listing.

To get the development of downloading, we name NSMetadataUbiquitousItemPercentDownloadedKey on our fileItem, which returns us the development as a Double worth.

Step 3, including a approach to get started download: Now, in the end upload a getBackup() means in our Restore.swift record, wherein we will be able to write the code to begin the download procedure.

That is curious about us to begin downloading of our iCloud record and exhibiting its development. Now, let’s check it out.

Step 4, including a button to begin the download: Let us now transfer to our ViewController.swift record and upload a brand new button to begin the download of the iCloud record. Add the next code on your viewDidLoad() means slightly under the code the place the uploadButton is added.

Now, let’s upload the motion for this button. Add the next means on your ViewController.swift record.

If you adopted all of the steps accurately, your ViewController.swift record must seem as follows:

All proper then! Everything appears excellent up to now, it’s time to now take a look at out the code that we have got written and spot it in motion.

Once we run the code and take a backup as described within the earlier article, lets test the record was once certainly uploaded to the iCloud force. Now, we’re going to transfer to our different telephone, login into the similar iCloud account with which we took the backup and get started downloading the record. You’ll see with some development the record is downloaded at the new tool and is visual in its iCloud listing.

Backing up our information to iCloud and restoring from iCloud is in reality now not that tricky if we apply the correct steps. I’m hoping I used to be ready that will help you with the method, let me know when you have any questions. Cheers!