Flávio
Flávio

Reputation: 111

Download .pdf or .ppt by WebView in Swift

I'm using the "webview" component to access the website. In an undetermined part of site, we have .pdf and .ppt format files. Can I download these files when the user clicks on them? Today, the application only opens .pdf and .ppt, but I would like to download it.

My code is in pt_BR.

class PortalAcademicoViewController: UIViewController {


    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        carregaSite()

    }


    func carregaSite(){
        /*
        let url = URL(string: "")
        var urlRequest = URLRequest(url: url!)
        urlRequest.cachePolicy = .returnCacheDataElseLoad
        webView.loadRequest(urlRequest) */


        //carregar o arquivo html
        let htmlFile = Bundle.main.path(forResource: "portalacademico", ofType: "html")
        let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
        HTTPCookieStorage.shared.cookieAcceptPolicy = .always
        self.webView.frame = self.view.bounds
        webView.loadHTMLString(html!, baseURL: Bundle.main.resourceURL)

        //voltar e avançar entres as paginas do webview
        let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
        let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))

        swipeLeftRecognizer.direction = .left
        swipeRightRecognizer.direction = .right

        webView.addGestureRecognizer(swipeLeftRecognizer)
        webView.addGestureRecognizer(swipeRightRecognizer)
        //fim

    }



    //trabalhando com avançar e voltar webview
    @objc private func handleSwipe(recognizer: UISwipeGestureRecognizer) {
        if (recognizer.direction == .left) {
            if webView.canGoForward {
                webView.goForward()
            }
        }

        if (recognizer.direction == .right) {
            if webView.canGoBack {
                webView.goBack()
            }
        }
    }
    //FIM

}

Upvotes: 1

Views: 4758

Answers (3)

Apfelsaft
Apfelsaft

Reputation: 5846

You can set a UIWebViewDelegate to your WebView and check for PDF and PPT files in webViewShouldStartLoadWithRequest. webViewShouldStartLoadWithRequest is called every time when the user clicks on a link.

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    if request for a PPT or PDF file {
        downloadFile()
        return false
    }
    return true
}

https://developer.apple.com/documentation/uikit/uiwebviewdelegate/1617945-webview?language=objc

Side note: You are currently using UIWebView which is deprecated. You should consider using WKWebView instead.

Upvotes: 1

UdayaLakmal
UdayaLakmal

Reputation: 4223

with WKWebView download .pdf and .ppt or any specific file, check my solution in here

https://stackoverflow.com/a/60614921/1025070

Upvotes: 0

Agula Otgonbaatar
Agula Otgonbaatar

Reputation: 111

I think you should perform download task with Javascript. Check your requests in func webView(_ webView: WKWebView, decidePolicyFor navigationAction ...) and if there download request found call the javascript method.

Here is someone already done pdf download with wkwebview.

https://forums.developer.apple.com/thread/108394

Upvotes: 2

Related Questions