0

我在云中有一个 15.2 kb 的文件。我无法将其复制回应用程序沙箱。我的代码在下面列出。任何帮助表示赞赏

    NSURL *ubiq = [[NSFileManager defaultManager]
                   URLForUbiquityContainerIdentifier:nil];
if (ubiq)
    {
    NSError *error = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *cloudContainerURL = [fileManager URLForUbiquityContainerIdentifier:nil]:
    NSURL*dirURL=[cloudContainerURL URLByAppendingPathComponent:@"Documents" isDirectory:YES];
    [fileManager createDirectoryAtURL:dirURL withIntermediateDirectories:NO  attributes:nil error:&error];
     NSURL *icloudURL = [dirURL URLByAppendingPathComponent:@"Passwords File.txt"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];

    // Destination path
    NSString *fileInDocumentsPath = [documentsPath  stringByAppendingPathComponent:@"Passwords File.txt"];
     NSURL* sandboxDocumentURL = [NSURL fileURLWithPath:fileInDocumentsPath];

  [[ NSFileManager defaultManager ] copyItemAtURL:icloudURL toURL:sandboxDocumentURL error:&error ];
4

1 回答 1

1

您不能只将文件从 iCloud 复制到本地目录,因为该文件可能尚不存在于本地设备上。您的应用程序的 iCloud 文件只会按需下载,而您没有做任何事情来创建这种需求。结果,没有要复制的源文件。即使您知道该文件在云中,您也无法访问它,直到它存在于本地。

如果您正在检查error传递给您的参数,copyItemAtURL:toURL:error:您可能已经知道这一点。

要下载您使用的文件-[NSFileManager startDownloadingUbiquitousItemAtURL:error:]。这开始了这个过程。您可以使用以下方法之一了解下载何时完成:

  • NSMetadataQuery,它将在下载完成时发布更新,或者
  • NSFileManager,它不会通知您,但确实有一个NSURLUbiquitousItemIsDownloadedKey您可以检查的标志。
于 2013-07-22T20:25:15.950 回答