我将多个文件备份到云(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
}
答案 0 :(得分:0)
在一位非常善良的开发人员的帮助下解决了问题。请注意评论“新”。现在,云文档文件夹中的结构应该是该文件夹中的所有文件。
- (void)moveNotesToCloud {
NSError *error;
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directoryToCopyFrom = [[searchPaths objectAtIndex:0] stringByAppendingPathComponent:@"Notes"];
NSLog(@"Move notes to iCloud, copy from: %@",directoryToCopyFrom);
NSString *filename;
NSDirectoryEnumerator *enumeratedDirectory = [fileManager enumeratorAtPath:directoryToCopyFrom];
NSURL *URLToBackup = [NSURL URLWithString:directoryToCopyFrom];
NSLog(@"Move notes to iCloud, backup url is: %@",URLToBackup);
while ((filename = [enumeratedDirectory nextObject])) {
if ([filename hasSuffix:@".html"]) {
// NEW
NSURL *srcURL = [URLToBackup URLByAppendingPathComponent:filename isDirectory:NO];
NSURL *iCloudDocumentsURL = [[fileManager URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:@"Documents"]; // Discovering iCloud URL
NSLog(@"Move notes to iCloud, doc url is: %@",iCloudDocumentsURL);
NSURL *destURL = [iCloudDocumentsURL URLByAppendingPathComponent:filename isDirectory:NO];
NSLog(@"Move notes to iCloud, dest url: %@",destURL);
// NEW
if ([fileManager setUbiquitous:YES itemAtURL:srcURL destinationURL:destURL error:&error] == FALSE)
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
[self performSelector:@selector(listNotesInThecloud) withObject:self afterDelay:5.0];
}