2013-06-01 58 views
2

我在将多个文件备份到云(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 

} 

回答

0

问题从一个很善良的开发人员的帮助下解决了。请注意“NEW”的评论。现在云文档文件夹中的结构应该是它所在的文件夹中的所有文件。

- (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]; 


}