O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

17.4. Searching for Files and Folders in iCloud

Problem

You want to search for files and/or folders inside the current iCloud user’s cloud space allocated for your app.

Solution

Use the NSMetadataQuery class.

Discussion

OS X Developers are probably familiar with the NSMetadataQuery class. This class allows developers to query Spotlight items, whether they are files or folders. In iOS, we will use this class to search for files and folders in the iCloud space assigned to the app for the current user, if she has set up iCloud for the iOS device on which the app is running.

To set up a metadata query, there are three very important things that we need to do:

  1. We need to set the predicate of the metadata query. The predicate is the search criteria of the query. This predicate will tell the query what items we are searching for.

  2. We also need to set the query’s search scope. In order to search in the user’s iCloud Documents folder, we set this scope to NSMetadataQueryUbiquitousDocumentsScope. Otherwise, you can use the NSMetadataQueryUbiquitousDataScope, which represents the Data folder in iCloud, a folder your app can use to store data related to the user-created documents. Remember that the files you store in the user’s iCloud should not be your app’s temporary files or any other files that your app can retrieve in some other way if those files weren’t present in the user’s iCloud storage. Things that you store in the user’s iCloud storage should be his or her creations.

  3. After we start the query, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required