在swift 4中的wkwebview中添加pdf的下载按钮

时间:2018-03-21 03:20:16

标签: swift4 ios11 wkwebview xcode9

我在xcode 9中使用wkwebiew完成了webview应用程序。在网站上,有一些部分需要下载pdf文件,pdf文件只能查看但如何使它可以下载到我们的iphone。你能和我分享一些小贴士吗,这是我的代码

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
}}

0 个答案:

没有答案