我在将多个文件备份到云(HTML文件)时遇到了问题。iCloud备份多个文件
每当我将文件备份到云中并从设备中移除时, 但在iCloud中的文档文件夹结构很奇怪:
的NSLog输出:
文件在云中:文件://本地主机/私人/.../移动%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/.../Mobil e%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
}