gpt4 book ai didi

ios - 如何使用文件管理器将 iCloud 中的文件复制到应用程序沙箱

转载 作者:行者123 更新时间:2023-11-29 03:45:50 24 4
gpt4 key购买 nike

我在云端有一个 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 ];

最佳答案

您不能直接将文件从 iCloud 复制到本地目录,因为该文件可能尚不存在于本地设备上。您的应用程序的 iCloud 文件仅按需下载,并且您没有执行任何操作来创建该需求。因此,没有可复制的源文件。即使您知道该文件位于云端,您也无法访问它,除非它存在于本地。

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

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

  • NSMetadataQuery,下载完成后将发布更新,或者
  • NSFileManager,它不会通知您,但有一个可供您检查的 NSURLUbiquitousItemIsDownloadedKey 标志。

关于ios - 如何使用文件管理器将 iCloud 中的文件复制到应用程序沙箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17775970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com