从iCloud Drive下载iOS文件

4
在我的iOS应用程序中,我将一些文件存储到iCloud Drive文件夹进行备份。现在我想检索该文件,但我不知道哪种方法是正确的。我的意思是是否有特定的iCloud Drive方法,还是我可以从URL获取它。
我像这样将文件存储到iCloud(https://dev59.com/B18d5IYBdhLWcg3wNQNg#27358392):
- (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];
}

我该如何从iCloud Drive下载文件?

提前感谢。

1个回答

2
您应该使用 NSMetadataQuery 来获取iCloud中的项目,然后当您为文件挂接了书签后,使用 NSFileCoordination 来读取它,相同的方法可用于从云端或您的应用沙盒中读取... 所有内容在这里都有详细说明:构建文档型应用程序,甚至还有一些示例代码供您参考。希望这能有所帮助!

谢谢回复。我看到的都是用Swift编写的。你知道有没有Objective C的文档或教程吗? - user3065901
通常在苹果的文档(以及其他地方)中,关于你想要的iOS和OSX的任何内容都很少... 我最好的猜测是从这里的NSMetadataQuery开始(有关于Swift和Objective-C的信息):https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMetadataQuery_Class/index.html 和文件元数据搜索编程指南:https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/Introduction.html#//apple_ref/doc/uid/TP40001841 这篇最后一篇文章只用Objective-C编写。 - gbdavid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接