【问题标题】:Download a pdf from Firebase and store it locally on iOS从 Firebase 下载 pdf 并将其存储在 iOS 本地
【发布时间】:2023-03-22 08:40:01
【问题描述】:

我需要从存储中下载 pdf 并将其保存在本地 iOS 设备上,以便可以在“文件”中看到它。

这里的代码取自我正在使用的文档:

override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let userID = Auth.auth().currentUser!.uid
        print(userID)

        // Get a reference to the storage service using the default Firebase App
        let storage = Storage.storage()

        // Create a storage reference from our storage service
        let storageRef = storage.reference()

        // Create a reference to the file you want to download
        let islandRef = storageRef.child("pdf/sample.pdf")

        // Create local filesystem URL
        let localURL = URL(string: "pdf/sample.pdf")!

        // Download to the local filesystem
        let downloadTask = islandRef.write(toFile: localURL) { url, error in
            if let error = error {
                // Uh-oh, an error occurred!
            } else {
                // Local file URL for "images/island.jpg" is returned
            }
        }

    }

当我尝试运行这个 ViewController 时,它不会崩溃,但会抛出以下错误:

"The file couldn’t be opened because the specified URL type isn’t supported." UserInfo={NSURL=pdf/sample.pdf}

Firebase 存储中的文件保存在名为 pdf/sample.pdf 的文件夹中。最后,我希望从存储中获取引用并将其传递给RealtimeDatabase,以便用户可以通过在表格视图中查看有关它的详细信息来下载它。

【问题讨论】:

  • 如果您选中此NSTransportSecurity,那么您的问题可能会得到解决。
  • @Emon 我在 info.plist 文件中进行了以下更改,我添加了此代码<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key><true/> </dict>。不幸的是,它不起作用。
  • 你仍然面临同样的错误?
  • @Emon 没有错误,但文件不会下载。
  • 现在检查您的网址,是不是您的预期网址?

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

我认为需要做的是指定要在本地文件系统的哪个路径中保存下载的文档。因此,假设您想使用临时文件夹来保存您的pdf。您可以尝试以下方法:

let tmporaryDirectoryURL = FileManager.default.temporaryDirectory
let localURL = tmporaryDirectoryURL.appendingPathComponent("sample.pdf")

islandRef.write(toFile: localURL) { url, error in
    if let error = error {
       print("\(error.localizedDescription)")
    } else {
       self.presentActivityViewController(withUrl: url)
    }
 }

下载文件后,您需要使用 UIActivityViewController 将其保存在“文件”应用中。

func presentActivityViewController(withUrl url: URL) {
    DispatchQueue.main.async {
      let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
      activityViewController.popoverPresentationController?.sourceView = self.view
      self.present(activityViewController, animated: true, completion: nil)
    }
}

我尚未对其进行测试,但我的假设是您收到此错误是因为您的 localURL 变量不是文件系统 URL。

【讨论】:

  • 收到以下错误:Cannot convert value of type 'URL' to expected argument type 'String'
  • 对不起。我更新了我的答案以直接使用 url 而不是重新创建它。
  • 进行了更改,没有错误。但下载过程并未启动。
  • 好的。我又做了一次编辑。添加downloadTask.resume()。正在进入完成块?
  • 完成块在必须打印URL时抛出错误,这里是错误load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://firebasestorage.googleapis.com/v0/b/project-6a073.appspot.com/o/pdf%2Fsample.pdf?alt=media, NSErrorFailingURLKey=https://firebasestorage.googleapis.com/v0/b/nameoftheproject-6a073.appspot.com/o/pdf%2Fsample.pdf?alt=media, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDownloadTask <AFC9BEB2-01FA-4B5B-BC6F-9A3C8F8A4893>.<2>" )我试过了,把错误缩短到这里。
【解决方案2】:

打开文件时应该使用URL(fileURLWithPath: String),而不是URL(string: String)

【讨论】:

  • 是否必须添加其他内容才能启动下载?
  • 你试过了吗?我不熟悉 Firebase API,但 URL 肯定是个问题。
  • 是的,我做到了。它不会记录错误,但不会启动下载。这是我对let localURL = URL(fileURLWithPath: "pdf/sample.pdf") 所做的更改。
  • @marosoaie 是对的,但我认为如果它仍然不起作用,那么您可能必须在 .plist 文件中添加 <key>NSAppTransportSecurity</key>
猜你喜欢
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 2021-06-24
  • 2017-01-14
相关资源
最近更新 更多