|
| 1 | +// |
| 2 | +// ArticleViewController.swift |
| 3 | +// Unwrap |
| 4 | +// |
| 5 | +// Created by Julian Schiavo on 2/5/2019. |
| 6 | +// Copyright © 2019 Hacking with Swift. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import WebKit |
| 11 | + |
| 12 | +class ArticleViewController: UIViewController, WKUIDelegate, WKNavigationDelegate { |
| 13 | + |
| 14 | + var article: NewsArticle |
| 15 | + |
| 16 | + let webView = WKWebView() |
| 17 | + let progressView = UIProgressView(progressViewStyle: .bar) |
| 18 | + var refreshButton: UIBarButtonItem? |
| 19 | + var shareButton: UIBarButtonItem? |
| 20 | + |
| 21 | + private var estimatedProgressObserver: NSKeyValueObservation? |
| 22 | + |
| 23 | + init(article: NewsArticle) { |
| 24 | + self.article = article |
| 25 | + super.init(nibName: nil, bundle: nil) |
| 26 | + |
| 27 | + extendedLayoutIncludesOpaqueBars = true |
| 28 | + |
| 29 | + webView.uiDelegate = self |
| 30 | + webView.navigationDelegate = self |
| 31 | + webView.allowsBackForwardNavigationGestures = true |
| 32 | + load(article: article) |
| 33 | + |
| 34 | + setupEstimatedProgressObserver() |
| 35 | + } |
| 36 | + |
| 37 | + required init?(coder aDecoder: NSCoder) { |
| 38 | + fatalError("init(coder:) has not been implemented") |
| 39 | + } |
| 40 | + |
| 41 | + override func loadView() { |
| 42 | + view = UIView() |
| 43 | + |
| 44 | + webView.translatesAutoresizingMaskIntoConstraints = false |
| 45 | + view.addSubview(webView) |
| 46 | + NSLayoutConstraint.activate([ |
| 47 | + webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), |
| 48 | + webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), |
| 49 | + webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), |
| 50 | + webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor) |
| 51 | + ]) |
| 52 | + |
| 53 | + progressView.isHidden = true |
| 54 | + progressView.translatesAutoresizingMaskIntoConstraints = false |
| 55 | + view.addSubview(progressView) |
| 56 | + |
| 57 | + NSLayoutConstraint.activate([ |
| 58 | + progressView.heightAnchor.constraint(equalToConstant: 2.0), |
| 59 | + progressView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), |
| 60 | + progressView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), |
| 61 | + progressView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor) |
| 62 | + ]) |
| 63 | + } |
| 64 | + |
| 65 | + override func viewDidLoad() { |
| 66 | + super.viewDidLoad() |
| 67 | + |
| 68 | + refreshButton = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(WKWebView.reload)) |
| 69 | + shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareArticle)) |
| 70 | + navigationItem.setRightBarButtonItems([shareButton!, refreshButton!], animated: true) |
| 71 | + } |
| 72 | + |
| 73 | + private func setupEstimatedProgressObserver() { |
| 74 | + estimatedProgressObserver = webView.observe(\.estimatedProgress, options: [.new]) { [weak self] webView, _ in |
| 75 | + guard let self = self else { return } |
| 76 | + self.progressView.setProgress(Float(webView.estimatedProgress), animated: true) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @objc private func shareArticle() { |
| 81 | + let alert = UIActivityViewController(activityItems: [article.title, article.url], applicationActivities: nil) |
| 82 | + alert.popoverPresentationController?.barButtonItem = shareButton |
| 83 | + present(alert, animated: true) |
| 84 | + } |
| 85 | + |
| 86 | + /// Loads a new article |
| 87 | + func load(article: NewsArticle) { |
| 88 | + self.article = article |
| 89 | + |
| 90 | + let request = URLRequest(url: article.url) |
| 91 | + webView.load(request) |
| 92 | + } |
| 93 | + |
| 94 | + // MARK: WKNavigationDelegate |
| 95 | + |
| 96 | + /// Allow all navigation but open third party urls |
| 97 | + func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { |
| 98 | + guard let url = navigationAction.request.url else { |
| 99 | + decisionHandler(.allow) |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + UIApplication.shared.open(url, options: [.universalLinksOnly: true]) { (successful) in |
| 104 | + if successful { |
| 105 | + decisionHandler(.cancel) |
| 106 | + } else { |
| 107 | + decisionHandler(.allow) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { |
| 113 | + title = article.title |
| 114 | + |
| 115 | + progressView.progress = 0 |
| 116 | + progressView.isHidden = false |
| 117 | + |
| 118 | + refreshButton?.isEnabled = false |
| 119 | + } |
| 120 | + |
| 121 | + func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { |
| 122 | + title = "Failed to load" |
| 123 | + refreshButton?.isEnabled = true |
| 124 | + } |
| 125 | + |
| 126 | + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
| 127 | + title = webView.title |
| 128 | + progressView.isHidden = true |
| 129 | + refreshButton?.isEnabled = true |
| 130 | + } |
| 131 | + |
| 132 | + // MARK: WKUIDelegate |
| 133 | + |
| 134 | + func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { |
| 135 | + webView.load(navigationAction.request) |
| 136 | + return nil |
| 137 | + } |
| 138 | +} |
0 commit comments