【发布时间】:2019-09-26 23:40:15
【问题描述】:
我正在尝试在“文件”应用中为我的应用创建一个文件夹,以便在我想从我的应用保存文件时访问
在 iOS 12 及更早的版本中,我可以通过启用来实现这一点
Application supports iTunes file sharing和Supports opening documents in place如下图
自从更新到 iOS 13 后,曾经在 iOS 12 中显示的同一个文件夹不再显示。
发生了什么变化?如何在 iOS 13 中解决这个问题?
我使用下面的扩展来保存文件:
extension WebViewController: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
// create destination URL with the original pdf name
guard let url = downloadTask.originalRequest?.url else { return }
let documentsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
// delete original copy
try? FileManager.default.removeItem(at: destinationURL)
// copy from temp to Document
do {
try FileManager.default.copyItem(at: location, to: destinationURL)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2){
let activityViewController = UIActivityViewController(activityItems: [self.fileName, destinationURL], applicationActivities: nil)
if(UIDevice.current.userInterfaceIdiom == .pad){
if let popOver = activityViewController.popoverPresentationController {
popOver.sourceView = self.view
popOver.sourceRect = self.view.bounds
popOver.barButtonItem = self.navigationItem.rightBarButtonItem
self.present(activityViewController, animated: true, completion: nil)
}
}
else{
self.present(activityViewController, animated: true, completion: nil)
}
}
} catch let error {
print("Copy Error: \(error.localizedDescription)")
}
}
}
【问题讨论】:
-
我存储 docx、pdf、xlsx。但即使在存储文件之前,通常“保存到文件”选项也会在文件应用程序中显示一个文件夹,其中包含我的应用程序名称
-
我已经编辑了我的问题以显示我如何保存文件
-
let documentsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]那不是 Documents 目录。文件应用显示您的 Documents 目录。 -
在 iOS 12 中这个工作有解释吗?