iCloud5_Building and Running Your App
搜索iCloud文件Searching for iCloud Documents
文件有一个设备上的一个应用创建,通过相同的应用运行在用户的其他设备上。一个应用发现文档在一个iCloud容器目录,是通过使用一个元数据查询对象,这是一个NSMetadatQuery对象的实例。一个元数据查询对象为文件查找可用的iCloud容器目录。Documents created by an app on one device must be discoverable by the same app running on the user’s other devices. An app discovers documents in an iCloud container directory is by using a metadata query object, which is an instance of the NSMetadataQuery class. A metadata query object searches the available iCloud container directories for files matching the criteria you specify.
因为文件会在任意时间出现在一个容器目录中(或者从目录中消失),它经常会使场景开启一个队列,并让它运行。在这种情况下,你应该在你应用的一些主要部分创建查询对象,并在你应用的声明周期与该对象保持一个关联。Because documents can appear in a container directory (or disappear from the directory) at any time, it often makes sense to start a query and leave it running. In such a case, you should create the query object in some central part of your app and keep a reference to that object for the lifetime of your app.
开始你的文档的寻找Starting Your Search for Documents
STEMasterViewController类在你应用声明的开始创建一个元数据查询对象,并打开它。这个查询会立刻返回一个文档的初始设置,当发生改变时发送修改。The STEMasterViewController class creates a metadata query object early in the life of the app and starts it. This query returns an initial set of documents right away and sends later updates when changes occur.
因为查询在应用运行过程中不断运行,STEMasterViewController类持续地使用一个实例变量来存储一个查询对象。Because the query runs constantly while the app is running, the STEMasterViewController class uses an instance variable to store a query object persistently.
-
在项目导航中,选择STEMasterViewController.mIn the project navigator, select
STEMasterViewController.m. -
添加一个称为_query的私有实例变量来存储元数据查询对象。Add a private instance variable called
_queryto store the metadata query object.变量的类型是一个指向NSMetadataQuery的指针。你的类的顶部实现,应该如下:The type of the variable is a pointer to
NSMetadataQuery. The top of your class implementation should now look like the following:@implementation STEMasterViewController {NSMutableArray *documents;
NSMetadataQuery *_query;
}
@synthesize addButton;
你在应用启动循环期间初始化_query实例变量。You initialize the _query instance variable during the app launch cycle.
-
在项目导航中,选择STEMasterViewController.mIn the project navigator, select
STEMasterViewController.m. -
定义一个新的被称为textDocumentQuery的方法来创建一个查询对象Define a new method called
textDocumentQueryto create the query object.这个方法的实现创建一个新的NSMetadataQuery对象,并配置它,看着像只是为在Document目录中的文档。这个方法的实现如下:The implementation of this method creates a new
NSMetadataQueryobject and configures it to look only for text documents in theDocumentsdirectory of the app’s iCloud containers. The implementation of this method is as follows:- (NSMetadataQuery*)textDocumentQuery {NSMetadataQuery* aQuery = [[NSMetadataQuery alloc] init];
if (aQuery) {// Search the Documents subdirectory only.
[aQuery setSearchScopes:[NSArray
arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
// Add a predicate for finding the documents.
NSString* filePattern = [NSString stringWithFormat:@"*.%@",
STEDocFilenameExtension];
[aQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@",
NSMetadataItemFSNameKey, filePattern]];
}
return aQuery;
}
-
定义一个新的setupAndStartQuery方法来打开查询。Define a new method called
setupAndStartQueryto start the query.这个方法检索元数据查询对象,配置通知处理程序,打开查询。这个方法的实现如下:The
setupAndStartQuerymethod retrieves the metadata query object, configures the notification handlers, and starts the query. The implementation of this method is as follows:- (void)setupAndStartQuery {// Create the query object if it does not exist.
if (!_query)
_query = [self textDocumentQuery];
// Register for the metadata query notifications.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processFiles:)
name:NSMetadataQueryDidFinishGatheringNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processFiles:)
name:NSMetadataQueryDidUpdateNotification
object:nil];
// Start the query and let it run.
[_query startQuery];
}
-
在文件的顶部添加一个setupAndStartQuery方法向前的声明Add a forward declaration of the
setupAndStartQuerymethod at the top of the file. -
修改awakeFromNib方法来调用setupAndStartQuery方法Update the
awakeFromNibmethod to call thesetupAndStartQuerymethod.你的awakeFromNib方法应该开起来类似如下:Your
awakeFromNibmethod should now look similar to the following:- (void)awakeFromNib {[super awakeFromNib];
if (!documents)
documents = [[NSMutableArray alloc] init];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[self setupAndStartQuery];
}
查询结果的过程Processing the Search Results
搜索结果在两个阶段传递:初始的收集阶段和修改阶段。初始阶段在你打开一个元数据查询就会立刻发生并且是第一组结果返回。之后,查询对象发送修改通知只在目录中的文件改变时。对于两个阶段,元数据查询结果包括所有在特定位置找到的文件。Search results are delivered in two phases: the initial gathering phase and the update phase. The initial gathering phase occurs immediately after you start a metadata query and is the first set of results to be returned. Thereafter, the query object sends update notifications only when the files in the directory change. For both the initial gathering phase and for updates, the metadata query results include all of the files currently found in the specified location.
简单文件编辑应用那个中,使用元数据查询通知来修改它的文档列表,刷新它的用户接口。The Simple Text Editor app uses the metadata query notifications to update its list of documents and refresh its user interface accordingly.
-
在项目导航中,选择STEMasterViewController.mIn the project navigator, select
STEMasterViewController.m. -
添加如下客户processFiles:方法的实现Add the following implementation of the custom
processFiles:method.这个方法创建一个发现的文件的列表,过滤应该被隐藏的文件。当处理一个搜索查询的结果,当你处理文件列表时,暂时禁用更新。这可以防止查询结果在你遍历它时发生改变。This method builds a list of discovered files, filtering out files that should be hidden. When processing the results of a search query, disable updates temporarily while you process the list of files. This prevents the query results from changing while you are iterating through them.
- (void)processFiles:(NSNotification*)aNotification {NSMutableArray *discoveredFiles = [NSMutableArray array];
// Always disable updates while processing results.
[_query disableUpdates];
// The query reports all files found, every time.
NSArray *queryResults = [_query results];
for (NSMetadataItem *result in queryResults) {NSURL *fileURL = [result valueForAttribute:NSMetadataItemURLKey];
NSNumber *aBool = nil;
// Don't include hidden files.
[fileURL getResourceValue:&aBool forKey:NSURLIsHiddenKey error:nil];
if (aBool && ![aBool boolValue])
[discoveredFiles addObject:fileURL];
}
// Update the list of documents.
[documents removeAllObjects];
[documents addObjectsFromArray:discoveredFiles];
[self.tableView reloadData];
// Reenable query updates.
[_query enableUpdates];
}
创建并运行你的应用Building and Running Your App
在这一点上,你应该有所有你需要构建并运行完整的应用程序。你应该能够在iCloud创建新文档,打开现有文档,编辑这些文档的内容。如果您有多个相关设备配置概要文件,你应该也可以插入每个设备,看到文件上创建一个出现在另一个。At this point, you should have everything you need to build and run the complete app. You should be able to create new documents in iCloud, open existing documents, and edit the contents of those documents. If you have multiple devices associated with the provisioning profile, you should also be able to plug in each device and see the files created on one show up on the other.
如果应用没有如期运行,你可以使用“Troubleshooting” 进行故障排除。If the app does not behave as you expect, you can troubleshoot the problem using the information in “Troubleshooting.”
扼要重述Recap
当你完成搜寻的实现,你已经完成了你的第三个iOS应用(一直在说的iCloud应用),祝贺!Now that you have completed the implementation of searching, you have completed your third iOS app. Congratulations!
花一会来考虑iCloud将如何影响你的应用框架。大部分需要支持iCloud的工作是在你的应用程序的数据部分。当你用一个UIDocument对象来管理你的文件,你所要做的工作量其实很小。Take a moment to think about how iCloud impacted your overall app architecture. Most of the work needed to support iCloud is in the data portion of your app. And when you use a UIDocument object to manage your files, the amount of work you have to do is actually small.

浙公网安备 33010602011771号