By MrPool

2019-04-15 10:10:52 8 Comments

I need to download a pdf from the storage and save it locally on an iOS device, so it can be seen in Files.

Here is the code is taken from the docs, which I'm using:

override func viewDidLoad() {

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

        // Get a reference to the storage service using the default Firebase App
        let 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


When I try to run this ViewController, it doesn't crash but throws the following error:

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

The file in the Firebase Storage is saved in a folder called pdf/sample.pdf. Eventually, I wish to take the reference from the storage and pass it in a RealtimeDatabase, so the user can download it by viewing details about it in a table view.


@axel 2019-04-15 10:57:08

I think what need to do is to specify in which path to your local filesystem you want to save the downloaded document. So let say you want to use the temporary folder to save your pdf. You can try the following:

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

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

Once the file is downloaded in order to save it in the Files app you will need to use 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)

I haven't tested it but my assumption is that you get this error because your localURL variable is not a filesystem URL.

@MrPool 2019-04-15 11:03:48

Getting the following error: Cannot convert value of type 'URL' to expected argument type 'String'

@axel 2019-04-15 11:05:47

Sorry. I updated my answer to use directly the url instead of re creating it again.

@MrPool 2019-04-15 11:13:49

Made the changes, no errors. But the download process isn't being initiated.

@axel 2019-04-15 11:21:16

Ok. I did one more edit. Add downloadTask.resume(). Are getting into the completion block?

@MrPool 2019-04-15 11:40:04

The completion block throws an error when it has to print the URL, here is the error load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://firebasestorage‌​‌​/project-6a073.appsp‌​‌​e.pdf?alt=media, NSErrorFailingURLKey=‌​v0/b/nameoftheprojec‌​‌​o/pdf%2Fsample.pdf?a‌​lt=media, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDownloadTask <AFC9BEB2-01FA-4B5B-BC6F-9A3C8F8A4893>.<2>" ) I've tried and shorten the error to add it here.

@MrPool 2019-04-15 11:58:59

The PDF file is present in the simulator data under tmp. It's not viewable on the device or its folders.

@axel 2019-04-15 12:00:36

Hmm. You mean even that the file is saved you also receive the error?

@axel 2019-04-15 12:05:29

@marosoaie 2019-04-15 10:24:44

Instead of using URL(string: String) you should be using URL(fileURLWithPath: String) when opening files.

@MrPool 2019-04-15 10:29:16

Would I have to add anything else to initiate the download?

@marosoaie 2019-04-15 10:31:47

Did you try it? I'm not familiar with the Firebase API, but the URL is certainly a problem.

@MrPool 2019-04-15 10:35:09

Yes, I did. It doesn't log the error, but it does not initiate the download. Here is the changes I made let localURL = URL(fileURLWithPath: "pdf/sample.pdf") .

@Emon 2019-04-15 10:37:25

@marosoaie is right but I think if still it does not work then may be you have to add <key>NSAppTransportSecurity</key> in .plist file.

Related Questions

Sponsored Content

6 Answered Questions

[SOLVED] How to store and view images on firebase?

0 Answered Questions

Uploading PDF files to Firebase

0 Answered Questions

get pdf from firebase storage

5 Answered Questions

[SOLVED] Open pdf file url which is stored in Firebase storage

2 Answered Questions

[SOLVED] Firebase Storage Download Response Error

1 Answered Questions

Firebase storage security

3 Answered Questions

1 Answered Questions

1 Answered Questions

file downloaded from Firebase storage get stored?

Sponsored Content