gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:08:47 25 4
gpt4 key购买 nike

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

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

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

编辑:

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

最佳答案

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

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,所以不能说它会被“批准”到应用程序商店(如果他们发现或关心...)

关于objective-c - 如何使用 iCloud 存储和同步应用程序文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7851039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com