iCloud 备份多个文件

Posted

技术标签:

【中文标题】iCloud 备份多个文件【英文标题】:iCloud Backing Up Multiple Files 【发布时间】:2013-06-01 11:56:39 【问题描述】:

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


【问题讨论】:

【参考方案1】:

在一位非常友善的开发人员的帮助下,问题得以解决。请注意 cmets 'NEW'。现在云 Documents 文件夹中的结构应该是该文件夹中的所有文件。

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



【讨论】:

以上是关于iCloud 备份多个文件的主要内容,如果未能解决你的问题,请参考以下文章

苹果手机icloud备份文件都包括啥

iCloud:禁用文件增量备份

iOS 阻止文件备份到 iCloud

Phonegap-iOS 防止 iCloud 上的 Documents 文件夹备份

用于 zip 文件和 iCloud 数据备份的 Md5

IOS 5.1 从文档目录备份/恢复应用程序文件到/从 iCloud