我想将我现有的应用程序迁移到 iCloud。
在我尝试学习该做什么的过程中,我尝试了一个示例应用程序,该应用程序已按照演示中的描述运行(如 iTunes 斯坦福大学课程的第 17 课所示)。
有这个问题的人可以帮我诊断我搞砸了什么步骤导致以下失败吗?这只是基本上告诉我没有要查询的数据吗?还是有其他事情我没有正确配置?
我在我的控制台中得到以下内容(在我调用后立即出现在下面的代码中[self.iCloudQuery startQuery];
item update error: 0, Error Domain=LibrarianErrorDomain Code=10 "The operation couldn’t be completed.
(LibrarianErrorDomain error 10 - Unable to configure the collection.)" UserInfo=0x11ee00 {NSDescription=Unable to
configure the collection.}
我创建了一个开发配置文件,在 iTunes 连接上打开了 iCloud。
我打开了权利。以下是内容:
代码直接来自斯坦福大学 iTunes 讲座:
#import "DocumentViewController.h"
@interface DocumentViewController ()
@property (strong, nonatomic) NSArray* documents; // of NSURLs
@property (strong, nonatomic) NSMetadataQuery* iCloudQuery;
@end
@implementation DocumentViewController
@synthesize documents = _documents;
@synthesize iCloudQuery = _iCloudQuery;
- (void)setDocuments:(NSArray*)documents {
// always sort documents alphabetically
documents
= [documents sortedArrayUsingComparator:^NSComparisonResult(NSURL* url1, NSURL* url2) {
return [[url1 lastPathComponent] caseInsensitiveCompare:[url2 lastPathComponent]];
}];
if (![documents isEqualToArray:_documents])
{
_documents = documents;
[self.tableView reloadData];
}
}
- (NSMetadataQuery*)iCloudQuery {
if (!_iCloudQuery)
{
_iCloudQuery = [[NSMetadataQuery alloc] init];
_iCloudQuery.searchScopes
= [NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope];
_iCloudQuery.predicate
= [NSPredicate predicateWithFormat:@"%K like '*'", NSMetadataItemFSNameKey];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processCloudQueryResults:)
name:NSMetadataQueryDidFinishGatheringNotification
object:_iCloudQuery];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processCloudQueryResults:)
name:NSMetadataQueryDidUpdateNotification
object:_iCloudQuery];
}
return _iCloudQuery;
}
#pragma mark - private implementation
- (NSURL*)iCloudURL {
return [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
}
- (NSURL*)iCloudDocumentsURL {
return [self.iCloudURL URLByAppendingPathComponent:@"Documents"];
}
- (NSURL*)filePackageURLForCloudURL:(NSURL*)url
{
if ([url.path hasPrefix:self.iCloudDocumentsURL.path])
{
NSArray* iCloudDocumentsURLComponents = self.iCloudDocumentsURL.pathComponents;
NSArray* urlComponents = url.pathComponents;
if (iCloudDocumentsURLComponents.count < urlComponents.count)
{
NSRange newRange = NSMakeRange(0, iCloudDocumentsURLComponents.count+1);
urlComponents = [urlComponents subarrayWithRange:newRange];
url = [NSURL fileURLWithPathComponents:urlComponents];
}
}
return url;
}
- (void)processCloudQueryResults:(NSNotification*)notification
{
[self.iCloudQuery disableUpdates];
NSMutableArray* documents = [NSMutableArray array];
NSUInteger resultCount = self.iCloudQuery.resultCount;
for (NSUInteger i = 0; i < resultCount ; ++i)
{
NSMetadataItem* item = [self.iCloudQuery resultAtIndex:i];
NSURL* url = [item valueForAttribute:NSMetadataItemURLKey];
url = [self filePackageURLForCloudURL:url];
[documents addObject:url];
}
self.documents = documents;
[self.iCloudQuery enableUpdates];
}
#pragma mark - overrides
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (![self.iCloudQuery isStarted])
[self.iCloudQuery startQuery];
[self.iCloudQuery enableUpdates];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.iCloudQuery disableUpdates];
[super viewWillDisappear:animated];
}