如何使用iCloud存储和同步应用程序文件

时间:2011-10-21 14:39:57

标签: objective-c cocoa-touch nsfilemanager icloud

我已经有一个iPhone应用程序将数据存储在本地文档文件夹中的文件中。现在我了解了iCloud技术,我的第一个问题是:当有时我检查新版本时,有没有办法将iCloud用作目录?

我的意思是:我可以避免使用UIDocument,文件协调员和文件演示者吗?我只想知道是否可以将iCloud视为特殊文件夹,并且只使用NSFileManager来推送和检索文件。

最后注意:我不使用Core Data或任何数据库,我只有一个数据文件。

修改

我已经阅读了正式的Apple iCloud文档,所以请不要将我链接到它们。我只需要一些代码示例。

5 个答案:

答案 0 :(得分:24)

对我来说“有效”的方法很简单:

NSFileManager *fm = [NSFileManager defaultManager];

NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

if (ubiq == nil) {
    return NO;
}

NSError *theError = nil;

[fm setUbiquitous:true itemAtURL:backupUrl destinationURL:[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:backupName] error:&theError];
Apple说要调用非UI线程。让文件“移动”。您可以通过NSMetaDataQuery查询这些内容,如下所示:

self.query = [[NSMetadataQuery alloc] init];
[self.query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K like '*.db'", NSMetadataItemFSNameKey];
[self.query setPredicate:pred];
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(queryDidFinishGathering:) 
                                             name:NSMetadataQueryDidFinishGatheringNotification 
                                           object:self.query];

[self.query startQuery];

- (void)queryDidFinishGathering:(NSNotification *)notification {
    NSMetadataQuery *query = [notification object];
    [query disableUpdates];
    [query stopQuery];

    [self loadData:query];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];

    self.query = nil; 
}

查询结果的枚举示例:

- (void)loadData:(NSMetadataQuery *)query {
    [self.backups removeAllObjects];

    for (NSMetadataItem *item in [query results]) {
        NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
        [self.backups addObject:url.lastPathComponent];
    }

    [_table reloadData];

    [self.loadingBackupIndicator stopAnimating];
    self.loadingIndicatorLabel.text = [NSString stringWithFormat: @"%d backups found", [self.backups count]];
}

开始“下载”具体文件:

NSFileManager *fm = [NSFileManager defaultManager];

NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

if (ubiq == nil) {
    return NO;
}

NSError *theError = nil;

bool started = [fm startDownloadingUbiquitousItemAtURL:[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:backupName] error:&theError];

NSLog(@"started download for %@ %d", backupName, started);

if (theError != nil) {
    NSLog(@"iCloud error: %@", [theError localizedDescription]);
}

检查文件“正在下载”:

- (BOOL)downloadFileIfNotAvailable {
    NSNumber *isIniCloud = nil;

    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

    NSURL *file = [[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:self.backupName];

    if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil]) {
        // If the item is in iCloud, see if it is downloaded.
        if ([isIniCloud boolValue]) {
            NSNumber*  isDownloaded = nil;
            if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil]) {
                if ([isDownloaded boolValue]) {
                    [self.loadingBackupIndicator stopAnimating];
                    self.loadingIndicatorLabel.text = @"Downloaded";

                    ....

                    [[NSFileManager defaultManager] copyItemAtPath:[file path] toPath:restorePath error:&theError ];

                    ....

                    return YES;
                }

                self.loadingCheckTimer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(downloadFileIfNotAvailable) userInfo:nil repeats:NO];
                [[NSRunLoop currentRunLoop] addTimer:self.loadingCheckTimer forMode:NSDefaultRunLoopMode];

                return NO;
            }
        }
    }

    return YES;
}

我没想到代码会那么长,并且抱歉在这里提供非常原始的代码段。无意说上述内容可以是代码的生产质量,只是分享这个概念。

我还没有将我的应用程序内容提交给Apple,所以不能说它会被“批准”到应用商店(如果他们发现或关心......)

答案 1 :(得分:15)

我知道你的感受,iCloud有点令人生畏。但是,我认为无法绕过UIDocument,文件协调员等,只需将iCloud用作简单的文件夹。

如果您正在寻找易于理解的示例代码,请查看此帖子:

iCloud basics and code sample

我提供了一个完整的示例代码,涵盖了iCloud的最低限度,并且几乎像目录一样使用它。也许这使您不必使用UIDocument,文件协调器等等。

但是,和你一样,我希望有一个更好的,更兼容的方式与良好的旧纪录片文件​​夹的想法。然而,由于这是iCloud,并且iCloud做了更多的事情(比如在不同的设备上保持所有内容同步,不断更新到云等),UIDocument等也无法实现。

答案 2 :(得分:14)

您可以使用NSFileManager将单个文件上传到iCloud。我在我的博客上发布了complete walkthrough如何做到这一点,但这是相关的NSFileManager代码:

NSURL *destinationURL = [self.ubiquitousURL URLByAppendingPathComponent:@"Documents/image.jpg"]
[[NSFileManager defaultManager] setUbiquitous:YES 
                                    itemAtURL:sourceURL
                               destinationURL:destinationURL
                                        error:&error]

答案 3 :(得分:6)

使用UIDocument并没有什么办法。我试图在我第一次使用iCloud时做到这一点,但事实证明这是一场没有UIDocument的灾难。最初使用UIDocument似乎需要做很多额外的工作,但事实并非如此。

您可以在一小时内轻松地将UIDocument子类化,并使其适用于任何类型的文件(只需将content属性设置为NSData)。它还提供了许多优于标准文件系统的好处:

  • 更改跟踪
  • 文件冲突解决方案
  • 文档状态支持
  • 增强的保存/打开/关闭功能

老实说,花一两个小时阅读Apple文档,然后使用它是非常值得的时间和脑力。有关iCloud文档存储的优秀入门文章可以在Apple's Developer Documentation中找到。


我编写了一个UIDocument子类,可以处理任何类型的文件(特别是NSData)。您可以在GitHub上查看,下载和修改UIDocument子类的代码。

创建文档:

// Initialize a document with a valid file path
iCloudDocument *document = [[iCloudDocument alloc] initWithFileURL:fileURL];

// Set the content of the document
document.contents = content;

// Increment the change count
[document updateChangeCount:UIDocumentChangeDone];

保存现有文件:

// Save and close the document
[document closeWithCompletionHandler:nil];

保存新文件:

[document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:nil];

您还可以使用NSMetadataQuery同步iCloud中存储的所有文件。 Apple提供了一个使用NSMetadata查询来同步应用程序文件的非常好的示例。还要确保在执行这些操作之前检查iCloud(提示:在NSFileManager上使用ubiquityIdentityToken方法)。


您可能还想考虑使用iCloud Document Sync等开源库。 iCloud Document Sync项目可以非常轻松地存储和同步应用程序文件:

  

使用单行代码方法将iCloud集成到iOS文档项目中。快速轻松地从iCloud同步,上传,管理和删除文档。有助于使iCloud对开发人员“正常工作”。

在几乎所有的iCloud Document Sync方法中,您所要做的就是将文件数据作为参数传递,然后处理其余部分(保存,同步等)。

免责声明:我是开源项目iCloud Document Sync的贡献开发人员。但是,我相信这个项目对你有益,并且与这个问题有关。这不是促销或广告。

答案 4 :(得分:-1)

使用此代码:

+ (NSString *)Pathsave {
    NSString *os5 = @"5.0";

    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedAscending) {
       // Lower than 4
        return path;
    } else if ([currSysVer compare:os5 options:NSNumericSearch] == NSOrderedDescending) {
        // 5.0.1 and above        
        return path;
    } else {
        // iOS 5
        path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
        return path;
    }

    return nil;
}