NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Open downloaded file in native app directly

Forums > Swift

Hey everybody!

I'm trying to build an app where the user clicks the download button, then in background the app downloads the file and opens it directly in the default native application.

I download the file usind this code below. It return the local URL to the file.

@IBAction func downloadFile(_ sender: Any) {
        downloadButton.isHidden = true
        loading.isHidden = false
        let url = URL(string: downloadURL!)!
        URLSession.shared.downloadTask(with: url) { location, response, error in
            guard let location = location, let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200 else { return }
            let fileName = httpURLResponse.suggestedFilename ?? httpURLResponse.url?.lastPathComponent ?? url.lastPathComponent
            let destination = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
            do {
                if FileManager.default.fileExists(atPath: destination.path) {
                    try FileManager.default.removeItem(at: destination)
                try FileManager.default.moveItem(at: location, to: destination)
                } catch {

And tried to open the file using this:

func pushFile(_ destination: URL) {
        var finalURL = destination.absoluteString

       DispatchQueue.main.async {
        if let url = URL(string: finalURL) {
                if #available(iOS 10, *){


How could i make it work?

Thank you for the help!


What happens when the app runs the openURL code? Nothing? Do you see anything in the Xcode console/output window?


@nemecek-filip nope nothing! It gives no output into console! Also nothing happens in the app.

I'm getting crazy trying to solve this. I can't find anything on the internet.


Are you sure pushFile is getting called? And URL(string: finalURL) produces url?


I added a print(url) after if let url = URL(string: finalURL) { and it prints into console: file:///Users/superc/Library/Developer/CoreSimulator/Devices/C1C47C37-17F3-4C56-B610-A03C17DEB73C/data/Containers/Data/Application/3E8AA4D1-94F1-4B85-80F8-B695E5F9CEFD/tmp/file.extension


Maybe part of the issue is that you dont have real extension like .pdf or .png. Also I would try this on a real device to see if anything changes. Simulator sometimes behaves strange.


I think the problem could be that the file is local. The file doesn't open even on real device.

But if i put like a pdf link to website like it runs and opens Safari.


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Ever ask for help and your reviewer immediately notices issues you missed? Fernando Olivares is a 10-year veteran from Big Nerd Ranch and Lambda School who can help hone your app building skills, and he's just launched a new book that does just that – use the code "hacking" to save $5!

Buy the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.