如何在使用UIDocumentPickerViewController选择并下载文档之前找到iCloud中文档的大小?

6

如何在选择并下载到设备之前,在iCloud中查找文档的大小,使用UIDocumentPickerViewController。我可以将文档下载到设备上,然后转换为NSData,我可以确定文件大小,但是我们能否避免下载文档本身并预先确定文件大小。我在uidocumentpickerviewcontroller中找不到任何方法或属性。

 - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {

 NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    NSError *error = nil;

    [coordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {

//assuming the file coordinator has downloaded the file here and provided the new url here
}

    }
1个回答

9

将文件协调器配置为仅读取元数据而不是下载整个文件。使用getPromisedItemResourceValue方法从此url中获取云中文件的文件大小:forKey:NSURLFileSizeKey error:

请发布任何比这种方式更有效的答案。这是对我有用的解决方案。

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
     NSError *error = nil;
    NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];

     [coordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly error:&error byAccessor:^(NSURL *newURL) {

         NSError *err = nil;

         NSNumber * fileSize;
         if(![url getPromisedItemResourceValue:&fileSize forKey:NSURLFileSizeKey error:&err]) {
             NSLog(@"Failed error: %@", error);
             return ;
         } else {
          NSLog(@"fileSize %f",fileSize.doubleValue);
}
}
}

1
你有没有任何想法如何针对文件夹做到这一点。我想获取文件夹中所有文件及其文件大小的列表。但是我不想下载它们中的任何一个。这可行吗?我已将文档选择器模式设置为“打开”,并尝试了您提到的方法。在调用startAccessingSecurityScopedResource开始访问安全范围资源后访问文件夹内容时,会抛出错误,表示所述文件夹不存在于该位置。 - Suran

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