add download button for pdf in wkwebview in swift 4

好久不见. 提交于 2021-02-07 13:38:23

问题


I have done webview app using wkwebiew in xcode 9. In the website, there are some part need to download the pdf file, the pdf file can be view only but how to make it can be download to our iphone. can you share some tip to me, this is my code

class ViewController: UIViewController,WKNavigationDelegate,UIWebViewDelegate {

@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var webView: WKWebView!
@IBOutlet var containerView: UIView? = nil
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var forwardButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    //webview
    self.view.addSubview(self.activityIndicator)
    self.view.addSubview(self.webView)
    let url:URL = URL(string : "https://www.facebook.com")!
    let urlRequest : URLRequest = URLRequest(url: url)
    webView.load(urlRequest)
    webView.navigationDelegate = self


    //activity indicator
    self.webView.addSubview(self.activityIndicator)
    self.activityIndicator.startAnimating()
    self.webView.navigationDelegate = self
    self.activityIndicator.hidesWhenStopped = true
    self.webView.navigationDelegate=self;


    //refresh
    webView.scrollView.bounces = true
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
    webView.scrollView.addSubview(refreshControl)

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear( animated )

    let url:URL = URL(string : "https://www.facebook.com")!
    let urlRequest : URLRequest = URLRequest(url: url)
    webView.load(urlRequest)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    activityIndicator.stopAnimating()
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
    activityIndicator.stopAnimating()
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    activityIndicator.startAnimating()
}

@objc func refreshWebView(sender: UIRefreshControl) {
    print("refersh")
    //
    sender.endRefreshing()
}

//back button
@IBAction func backButtonTapped(_ sender: Any) {
    if webView.canGoBack {
        webView.goBack()
    }
}

@IBAction func forwardButtonTapped(_ sender: Any) {
    if webView.canGoForward {
        webView.goForward()
    }
}

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
    backButton.isEnabled = webView.canGoBack
    forwardButton.isEnabled = webView.canGoForward
}}

来源:https://stackoverflow.com/questions/49397579/add-download-button-for-pdf-in-wkwebview-in-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!