MrPool
MrPool

Reputation: 401

Download a pdf from Firebase and store it locally on iOS

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

    }

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.

Upvotes: 1

Views: 1508

Answers (2)

axel
axel

Reputation: 422

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 {
       print("\(error.localizedDescription)")
    } 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.

Upvotes: 1

marosoaie
marosoaie

Reputation: 2371

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

Upvotes: 0

Related Questions