1.removeItemAtURL删除本地文件document。
2。创建子目录来存储文件。
self.ubiquityURL = [[fMgr URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents"];
3。创建文档使用self.ubiquityURL目录
4。在iCloud中使用set谓词document.doc搜索:[NSpredicate predicateWithFormat:@“%K like 'document.doc'”,NSMetadataItemFSNameKey];

NSMetaDataItemFSSizeKey, NSMetaDataItemFSCreationDataKey, NSMetaDataItemFSContentChangeDataKey
5.Then设置搜索范围NSMetaDataQueryUbiquitysDocumentsScope的;
[self.metaDataQuery setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
PS:另外一个搜索数据:NSMetaDataQueryUbiquitysDataScope另一个范围;
6。开始查询[self.metaDataQuery startQuery];
7。设置一个通知,当搜索查询完成:NSMetaDataQueryDidfinishGatheringNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(metaDataQueryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:self.metaDataQuery];

8.用metaDataQueryDidfinishGather继续
a. NSMetadataQuery *query = [notificaiton object];
b.在查询中停止通知 remove the notification from query;
c。停止查询; stop query;
9。得到的结果对象从[查询结果] got the result object from [query result]
NSArray *results = [[NSArray alloc] initWithArray:[query results] ];
10. check if the [result count] == 1. if yes, got the ubiquityURL and document.doc using:
The important thing is if there’s only search a specific file, then the first file is what we need.
self.ubiquityURL = [result[0] valueForAttribute:NSMetaDataItemURLKey];
self.document = [[BKDocument alloc] initWithFileURL:self.ubiquityURL];
[self.document openWithCompletionHandler:^(BOOL success) {
if (success) {
self.textView.text = self.document.userText;
}else{
NSLog(@"Faild to open");
}
}]
11.if there’s not any result, then create it using saveToURL:
UIDocumentSaveForOverWriting,UIDocumentSaveForCreating
12. Finally Click the Save button to save text to document;
//
// BKViewController.h
// BKDocument
//
// Created by xushao on 13-4-5.
// Copyright (c) 2013年 xushao. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "BKDocument.h"
@interface BKViewController : UIViewController
@property(nonatomic, strong)BKDocument *document;
@property(nonatomic, strong) NSURL *documentURL;
@property(nonatomic, strong)NSMetadataQuery *metaDataQuery;
@property(nonatomic, strong)NSURL *ubiquityURL;
@property (strong, nonatomic) IBOutlet UITextView *textView;
- (IBAction)savebutton:(id)sender;
@end
//
// BKViewController.m
// BKDocument
//
// Created by xushao on 13-4-5.
// Copyright (c) 2013年 xushao. All rights reserved.
//
#import "BKViewController.h"
@interface BKViewController ()
@end
@implementation BKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dataPath = filePath[0];
NSString *path = [[NSString alloc] stringByAppendingPathComponent:dataPath];
NSFileManager *fMgr = [NSFileManager defaultManager];
NSError *error;
[fMgr removeItemAtPath:path error:&error];
self.ubiquityURL = [[fMgr URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents"];
if ([fMgr fileExistsAtPath:[self.ubiquityURL path]]==NO) {
[fMgr createDirectoryAtURL:self.ubiquityURL withIntermediateDirectories:YES attributes:nil error:nil];
}
self.ubiquityURL = [self.ubiquityURL URLByAppendingPathComponent:@"document.doc"];
self.metaDataQuery = [[NSMetadataQuery alloc] init];
[self.metaDataQuery setPredicate:[NSPredicate predicateWithFormat:@"%K like 'document.doc'",NSMetadataItemFSNameKey]];
[self.metaDataQuery setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
[self.metaDataQuery startQuery];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(metaDataQueryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:self.metaDataQuery];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)metaDataQueryDidFinishGathering:(NSNotification *)notificaiton
{
NSMetadataQuery *query = [notificaiton object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
[query stopQuery];
NSArray *results = [[NSArray alloc] initWithArray:[query results] ];
if ([results count]==1) {
self.ubiquityURL = [results[0] valueForAttribute:NSMetadataItemURLKey];
self.document = [[BKDocument alloc] initWithFileURL:self.ubiquityURL];
[self.document openWithCompletionHandler:^(BOOL success) {
if (success) {
self.textView.text = self.document.userText;
}else{
NSLog(@"Faild to open");
}
}];
}else{
self.document = [[BKDocument alloc] init];
[self.document saveToURL:self.ubiquityURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Create Success");
}else{
NSLog(@"Couldn't create file");
}
}];
}
}
- (IBAction)savebutton:(id)sender {
self.document.userText = self.textView.text;
[self.document saveToURL:self.ubiquityURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Over writing success");
}else{
NSLog(@"Over writing failed");
}
}];
}
@end