我在将多个文件备份到云(HTML 文件)时遇到问题。
每当我备份文件时,它们都会放在云端并从我的设备中删除。但是iCloud 的 Documents 文件夹中的结构很奇怪:
NSLog 输出:
云端文件:file://localhost/private/.../Mobile%20Documents/...~com~nwt/Documents/File1_01-06-2013.html/ 云端文件:file://localhost/ private/.../Mobile%20Documents/...~com~nwt/Documents/File1_01-06-2013.html/File1_01-06-2013.html 云端文件:file://localhost/private/.. ./Mobile%20Documents/...~com~nwt/Documents/File1_01-06-2013.html/File2_01-06-2013.html 云端文件:file://localhost/private/.../Mobile% 20Documents/...~com~nwt/Documents/File1_01-06-2013.html/File3_01-06-2013.html 云端文件:file://localhost/private/.../Mobile%20Documents/.. .~com~nwt/Documents/Rbi8Bookmarks.plist
正如您在第一个实例中看到的那样,它为第一个 html 文件创建了一个文件夹,然后将完整的 html 文件数组嵌套在第一个文件名的文件夹中。但对于单个文件(plist)则不然。
有任何想法吗?我整个星期都在研究这个问题,但我无处可去。
菲利普
这是我的代码:
- (IBAction)backupToiCloud:(id)sender {
NSString *directoryToCopyFrom = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"Notes"];
NSDirectoryEnumerator *enumeratedDirectory = [fileManager enumeratorAtPath:directoryToCopyFrom];
NSString *filename;
while ((filename = [enumeratedDirectory nextObject])) {
if ([filename hasSuffix:@".html"]) {
// First, copy the note files to the documents directory as a backup to be copied back after cloud backup is finished
[fileManager copyItemAtPath:[directoryToCopyFrom stringByAppendingPathComponent:filename]
toPath:[documentFolderPath stringByAppendingPathComponent:filename] error:&error];
NSLog(@"Files on my phone: %@", filename);
[self performSelector:@selector(moveNotesToCloud) withObject:self afterDelay:3];
}
}
}
- (void)moveNotesToCloud {
NSError *error;
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directoryToCopyFrom = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"Notes"];
NSString *filename;
NSDirectoryEnumerator *enumeratedDirectory = [fileManager enumeratorAtPath:directoryToCopyFrom];
NSURL *URLToBackup = [NSURL URLWithString:directoryToCopyFrom];
while ((filename = [enumeratedDirectory nextObject])) {
if ([filename hasSuffix:@".html"]) {
NSURL *iCloudDocumentsURL = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents"]; // Discovering iCloud URL
NSURL *destURL = [iCloudDocumentsURL URLByAppendingPathComponent:filename];
[fileManager setUbiquitous:YES itemAtURL:URLToBackup destinationURL:destURL error:&error];
NSLog(@"Notes copied to the cloud: %@", error);
NSString *path = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"Notes"]; // Does directory already exist?
[fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
}
} // end WHILE statement
// Wait 5 seconds for the cloud to register the changes and then list them in the console
[self performSelector:@selector(listNotesInThecloud) withObject:self afterDelay:5.0];
}
// THIS JUST LISTS THE FILES IN THE CLOUD
- (void)listNotesInThecloud {
NSURL *iCloudDocumentsURL = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents"]; // Discovering iCloud URL
NSDirectoryEnumerator *enumeratedDirectory = [fileManager enumeratorAtPath:iCloudDocumentsURL.path];
NSString *filename;
while ((filename = [enumeratedDirectory nextObject])) {
if ([filename hasSuffix:@".html"] || [filename hasSuffix:@".plist"]) {
NSURL *destURL = [iCloudDocumentsURL URLByAppendingPathComponent:filename];
NSLog(@"Files in the cloud: %@", destURL);
}
} // end WHILE statement
}