1

我想将我现有的应用程序迁移到 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];
}
4

1 回答 1

4

对于遇到此问题的其他任何人......显然,这是当您没有在您正在测试的设备上启用 iCloud 时发生的错误。一旦我在设备上启用了 iCloud,错误就停止在我的日志中发生。

所以现在我知道要检查什么错误才能向用户显示一个对话框,说“连接到 iCloud 将无法工作,直到你在系统偏好中注册 iCloud”。

于 2012-07-17T09:10:03.773 回答