UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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)
                self.pushFile(destination)
                } catch {
                    print(error)
                }
        }.resume()
    }

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, *){
                    UIApplication.shared.open(url)
                }else{
                    UIApplication.shared.openURL(url)
                }

        }
    }

How could i make it work?

Thank you for the help!

2      

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

2      

@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.

2      

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

2      

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

2      

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.

2      

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 https://www.liceopascolibz.it/portalescuola/docenti/alessiofilippi/wp-content/uploads/2007/03/dispense-funzioni-lineari.pdf it runs and opens Safari.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.