iOS从iCloud驱动器下载文件

iOS从iCloud驱动器下载文件,ios,file,download,icloud-drive,Ios,File,Download,Icloud Drive,在我的iOS应用程序中,我将一些文件存储到iCloud驱动器文件夹中进行备份。现在我想检索该文件,但我不知道哪种方法是正确的。我的意思是,如果iCloud Drive有任何特定的方法,或者我可以从url获取 我将文件存储到iCLoud,如下所示(): 如何从iCloud驱动器下载文件 提前感谢。您应该使用NSMetadataQuery从icloud获取该项目,然后当您将书签挂接到文件时,使用NSFileCoordination读取该项目,同样的方法也可以用于从云或应用程序沙盒读取该项目。。。这里

在我的iOS应用程序中,我将一些文件存储到iCloud驱动器文件夹中进行备份。现在我想检索该文件,但我不知道哪种方法是正确的。我的意思是,如果iCloud Drive有任何特定的方法,或者我可以从url获取

我将文件存储到iCLoud,如下所示():

如何从iCloud驱动器下载文件


提前感谢。

您应该使用NSMetadataQuery从icloud获取该项目,然后当您将书签挂接到文件时,使用NSFileCoordination读取该项目,同样的方法也可以用于从云或应用程序沙盒读取该项目。。。这里详细解释了所有内容:,甚至还有一些示例代码可以遵循。希望这有帮助

谢谢你的回复。我看到的都是用swift写的。你知道Objective C中的任何文档或教程吗?苹果的文档(以及其他地方)中通常很少有关于ios和osx的任何内容。。。我最好的猜测是从NSMetadataQuery(这里有swift和objective-c的信息)和文件元数据搜索编程指南开始:最后一篇文章仅用objective-c编写
- (void) storeToIcloud
{
   // Let's get the root directory for storing the file on iCloud Drive
   [self rootDirectoryForICloud:^(NSURL *ubiquityURL) {

        if (ubiquityURL) {

             // We also need the 'local' URL to the file we want to store

             NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
             NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
             NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myFile.xml"]; //Add the file name

             NSURL *localURL = [NSURL fileURLWithPath:filePath];

             // Now, append the local filename to the ubiquityURL
             ubiquityURL = [ubiquityURL URLByAppendingPathComponent:localURL.lastPathComponent];

             // And finish up the 'store' action
             NSError *error;
             if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:ubiquityURL error:&error]) {
                NSLog(@"Error occurred: %@", error);
             }
        }
        else {
            NSLog(@"Could not retrieve a ubiquityURL");
        }
   }];
}

- (void)rootDirectoryForICloud:(void (^)(NSURL *))completionHandler {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *rootDirectory = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"];

        if (rootDirectory) {
             if (![[NSFileManager defaultManager] fileExistsAtPath:rootDirectory.path isDirectory:nil]) {
                  NSLog(@"Create directory");
                  [[NSFileManager defaultManager] createDirectoryAtURL:rootDirectory withIntermediateDirectories:YES attributes:nil error:nil];
             }
        }

        dispatch_async(dispatch_get_main_queue(), ^{
             completionHandler(rootDirectory);
        });
    });
}

- (NSURL *)localPathForResource:(NSString *)resource ofType:(NSString *)type {
      NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
      NSString *resourcePath = [[documentsDirectory stringByAppendingPathComponent:resource] stringByAppendingPathExtension:type];
      return [NSURL fileURLWithPath:resourcePath];
}