从 UIDocumentPicker 下载文件并等待下载完成

     2023-03-08     120

关键词:

【中文标题】从 UIDocumentPicker 下载文件并等待下载完成【英文标题】:Download files from UIDocumentPicker and wait until download has finished 【发布时间】:2015-02-23 18:53:29 【问题描述】:

我正在尝试从以下位置下载文件

-documentPicker:didPickDocumentAtURL:

方法。 我尝试使用

获取文件的数据
NSData *data = [NSData dataWithContentsOfURL:url];

但它没有按我的预期工作,因为 UIDocumentPicker 在文件下载之前触发 -documentPicker:didPickDocumentAtURL: -方法。

下载文件后如何从文件中获取 NSData?

在此先感谢您,费边。

【问题讨论】:

【参考方案1】:

文件实际上是下载的,但是您需要使用文件协调器来读取文件内容。应该这样做:

- (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) 
        NSData *data = [NSData dataWithContentsOfURL:newURL];
        // Do something
    ];
    if (error) 
        // Do something else
    

【讨论】:

您不必使用 NSFileCoordinator,因为此委托调用似乎是同步的,并且 dataWithContents 工作得非常好。 好吧,这对他不起作用。另外,我认为避免使用 NSFileCoordinator 会留下太多的机会。苹果表示,“所有与文件相关的操作都必须通过文件协调器对象来执行。” developer.apple.com/library/ios/documentation/FileManagement/… 没错。但随着 iOS 更新到 iOS 8.3,UIDocumentPicker 会在关闭之前下载显示活动指示器的文件。当 UIDocumentPicker 实际被解除时,我可以在没有 NSFileCoordinator 的情况下访问该文件。 当然,除非它在您访问它时发生变化。 iCloud 同步可以随时发生。顺便说一句,我确实注意到了活动指示器,并且很高兴看到它。 你能帮忙***.com/questions/30613645/…

我应该删除使用 UIDocumentPicker 导入的文件吗?

】我应该删除使用UIDocumentPicker导入的文件吗?【英文标题】:ShouldIremovefilesimportedwithUIDocumentPicker?【发布时间】:2017-01-0917:25:35【问题描述】:所以我正在使用UIDocumentPicker在我的应用中导入文件letdocumentMenu=UIDocumentMenuViewController... 查看详情

无法使用 UIViewRepresentable 和 UIDocumentPicker 选择文件夹

】无法使用UIViewRepresentable和UIDocumentPicker选择文件夹【英文标题】:Can\'tselectafolderwithUIViewRepresentable&UIDocumentPicker【发布时间】:2021-01-2823:03:53【问题描述】:我正在尝试为用户创建一个选择器,以便在SwiftUI应用程序中选择一... 查看详情

如何使用 UIDocumentPicker 将 pdf 文件导入表格视图单元格

】如何使用UIDocumentPicker将pdf文件导入表格视图单元格【英文标题】:HowtoimportpdffilestotableviewcellsusingUIDocumentPicker【发布时间】:2018-04-1605:59:00【问题描述】:我真的很纠结如何导入pdf文件并将其存储在表格视图单元格中。我正在... 查看详情

在 iOS 中导入文件:UIDocumentPicker 与 UIDocumentBrowser

】在iOS中导入文件:UIDocumentPicker与UIDocumentBrowser【英文标题】:ImportingfilesiniOS:UIDocumentPickervsUIDocumentBrowser【发布时间】:2018-09-1018:07:43【问题描述】:根据文档,UIDocumentPickerViewController和UIDocumentBrowserViewController都可用于在应用... 查看详情

使用自定义 UTI 删除 UIDocumentPicker 导入/导出

】使用自定义UTI删除UIDocumentPicker导入/导出【英文标题】:DeleteUIDocumentPickerimport/exportwithcustomUTIs【发布时间】:2014-09-2318:08:55【问题描述】:我有一个应用程序可以使用自定义文件(带有自定义扩展名/UTI)存储用户创建的内容... 查看详情

无法使用 UIDocumentPicker 导入页面和编号文档

】无法使用UIDocumentPicker导入页面和编号文档【英文标题】:UnabletoimportpagesandnumbersdocumentswithUIDocumentPicker【发布时间】:2015-03-1209:23:48【问题描述】:我正在开发一个能够存储云文档的应用。此应用可以选择使用新的UIDocumentPicker... 查看详情

UIDocumentPicker 导航栏按钮在 iOS 11 中隐藏

】UIDocumentPicker导航栏按钮在iOS11中隐藏【英文标题】:UIDocumentPickernavigationbarbuttonsarehiddenatiOS11【发布时间】:2017-11-2612:21:17【问题描述】:我注意到我的UIDocumentPicker的导航栏中仅在iOS11中存在问题,完成、取消或编辑按钮是不... 查看详情

快速在 UIDocumentPicker 中设置文档扩展名

】快速在UIDocumentPicker中设置文档扩展名【英文标题】:SettingdocumentextensionsinUIDocumentPickerinswift【发布时间】:2018-08-2906:34:45【问题描述】:我正在使用文档选择器来选择文档并上传到服务器。我只需要为允许的文档传递一些扩展... 查看详情

iCloud UIDocumentPicker 间歇性崩溃并挂起

】iCloudUIDocumentPicker间歇性崩溃并挂起【英文标题】:iCloudUIDocumentPickercrashesintermittentlyandhangs【发布时间】:2015-03-1816:30:43【问题描述】:问题:当从iCloud中选择一个文档时,应用程序会随机崩溃,在大多数情况下,以下代码可... 查看详情

从 BD 下载文件

】从BD下载文件【英文标题】:DownloadFilefromBD【发布时间】:2018-11-3016:06:31【问题描述】:如何从db而不是从某个路径下载文件?我正在以这种方式上传文件:[HttpPost,ActionName("CandidateCreate")][ValidateAntiForgeryToken]publicIActionResultCandida... 查看详情

每天自动从网站下载文件

】每天自动从网站下载文件【英文标题】:Automaticallydownloadafilefromawebsiteeveryday【发布时间】:2022-01-2301:53:48【问题描述】:我需要每天自动从这个网站下载一个文件:“https://es.investing.com/indices/merv-historical-data”。要下载文件,... 查看详情

FileProvider - 从下载目录打开文件

】FileProvider-从下载目录打开文件【英文标题】:FileProvider-OpenFilefromDownloadDirectory【发布时间】:2017-08-2312:51:36【问题描述】:我无法从下载文件夹中打开任何文件。我可以用这个下载文件并保存在下载文件夹中:DownloadManager.Requ... 查看详情

从 Azure 文件共享下载和删除文件

】从Azure文件共享下载和删除文件【英文标题】:DownloadanddeletefilefromAzurefileshares【发布时间】:2021-12-0609:42:32【问题描述】:我有以下代码可以将文件从Azure文件共享下载到本地,并且工作正常。有什么办法,下载完成后可以删... 查看详情

从方法 startDownloadingUbiquitousItem 下载的文件是哪个文件夹?

】从方法startDownloadingUbiquitousItem下载的文件是哪个文件夹?【英文标题】:WhichfolderisthefilesdownloadedfrommethodstartDownloadingUbiquitousItem?【发布时间】:2020-02-0407:18:02【问题描述】:方法下载的文件是什么文件夹?我找不到从这个方法... 查看详情

iOS 从 url 下载 html 文件

】iOS从url下载html文件【英文标题】:iOSDownloadhtmlfilefromurl【发布时间】:2014-09-0108:01:55【问题描述】:我需要从服务器url下载html文件并替换为本地html文件。我正在使用AFNetworking下载文件并存储到Documents文件夹。它正在下载视频... 查看详情

从 FTP 上传文件和下载文件

】从FTP上传文件和下载文件【英文标题】:UploadfileanddownloadfilefromFTP【发布时间】:2012-10-1801:41:45【问题描述】:我正在尝试制作一个将.exe文件上传/下载到FTP的程序我尝试使用FtpWebRequest,但我只能成功上传和下载.txt文件。然后... 查看详情

从服务器一个一个下载文件

】从服务器一个一个下载文件【英文标题】:Downloadfilefromserveronebyone【发布时间】:2017-02-1306:20:42【问题描述】:我必须在UITableViewCell中单击相应按钮时从服务器下载文件。当用户点击按钮以在一个单元格中下载时,应该开始下... 查看详情

从网站下载 zip 文件时出错

】从网站下载zip文件时出错【英文标题】:Errorwhiledownloadingazipfilefromawebsite【发布时间】:2011-03-2622:28:58【问题描述】:我正在尝试使用Web客户端从网站下载zip文件,但该文件未正确下载。每次下载没有内容的zip文件时,在目标... 查看详情