對我來說「有效」只是簡單的:
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];
蘋果說要調用非UI線程。讓文件「移動」。
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]);
}
您可以通過NSMetaDataQuery
這樣的查詢他們檢查文件「正在下載」:
- (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;
}
我沒想到代碼會很長,並且很抱歉在這裏提供非常原始的代碼片斷。沒有意圖說出上述可以是代碼的生產質量,只是分享這個概念。
我還沒有提交了我的應用程序的蘋果裏面,所以看不出來,這將是「認可」的應用程序商店(如果他們發現或護理...)
我同意,如果你說沒有辦法,我會閱讀教程。但我認爲應該有一種更簡單的方法來做到這一點,就像在應用沙箱中編寫或讀取文件時一樣。謝謝 – edo42
感謝您的鏈接。 – Shajo