【问题标题】:how to find the size of a document in iCloud before picking it and downloading to device using UIDocumentPickerViewController如何在选择文档并使用 UIDocumentPickerViewController 下载到设备之前在 iCloud 中查找文档的大小
【发布时间】:2016-11-29 14:09:13
【问题描述】:

如何在选择文档并使用 UIDocumentPickerViewController 下载到设备之前在 iCloud 中查找文档的大小。我可以将文档下载到设备然后转换为 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
}

    }

【问题讨论】:

    标签: ios icloud uidocumentpickervc


    【解决方案1】:

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

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

    - (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);
    }
    }
    }
    

    【讨论】:

    • 您知道如何为文件夹执行此操作吗?我想获取文件夹中所有文件的列表及其文件大小。但我不想下载其中任何一个。这可能吗?我已将文档选择器模式设置为open,并尝试了您提到的方法。在调用 start startAccessingSecurityScopedResource 后访问文件夹内容会引发错误,指出该位置中不存在提到的文件夹。
    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 2018-12-31
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2017-11-18
    • 2015-05-03
    相关资源
    最近更新 更多