|
7 | 7 | // |
8 | 8 |
|
9 | 9 | import UIKit |
| 10 | +import WebKit |
10 | 11 |
|
11 | 12 | class ViewController: UIViewController { |
| 13 | + |
| 14 | + // MARK: Outlets |
| 15 | + @IBOutlet weak var leftButton: UIBarButtonItem! |
| 16 | + @IBOutlet weak var rightButton: UIBarButtonItem! |
| 17 | + @IBOutlet weak var webViewContainer: UIView! |
| 18 | + // MARK: Globals |
| 19 | + var webView: WKWebView! |
| 20 | + var progressBar : UIProgressView! |
| 21 | + let appUrl = URL(string: "https://www.duckduckgo.com/") |
| 22 | + let allowedOrigin = "duckduckgo.com" |
12 | 23 |
|
13 | 24 | override func viewDidLoad() { |
14 | 25 | super.viewDidLoad() |
15 | 26 | // Do any additional setup after loading the view, typically from a nib. |
| 27 | + setupApp() |
16 | 28 | } |
17 | 29 |
|
18 | 30 | override func didReceiveMemoryWarning() { |
19 | 31 | super.didReceiveMemoryWarning() |
20 | 32 | // Dispose of any resources that can be recreated. |
21 | 33 | } |
| 34 | + |
| 35 | + // UI Actions |
| 36 | + @IBAction func onLeftButtonClick(_ sender: Any) { |
| 37 | + if (webView.canGoBack) { |
| 38 | + webView.goBack() |
| 39 | + } |
| 40 | + } |
| 41 | + @IBAction func onRightButtonClick(_ sender: Any) { |
| 42 | + webView.evaluateJavaScript("alert('boom')", completionHandler: nil) |
| 43 | + } |
| 44 | + |
| 45 | + // Observers for updating UI |
| 46 | + override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { |
| 47 | + if (keyPath == #keyPath(WKWebView.isLoading)) { |
| 48 | + leftButton.isEnabled = webView.canGoBack |
| 49 | + } |
| 50 | + if (keyPath == #keyPath(WKWebView.estimatedProgress)) { |
| 51 | + progressBar.progress = Float(webView.estimatedProgress) |
| 52 | + rightButton.isEnabled = (webView.estimatedProgress == 1) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Initialize WKWebView |
| 57 | + func setupWebView() { |
| 58 | + // set up webview |
| 59 | + webView = WKWebView(frame: CGRect(x: 0, y: 0, width: webViewContainer.frame.width, height: webViewContainer.frame.height)) |
| 60 | + webView.navigationDelegate = self |
| 61 | + webView.uiDelegate = self |
| 62 | + webViewContainer.addSubview(webView) |
| 63 | + webView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 64 | + |
| 65 | + // settings |
| 66 | + webView.allowsBackForwardNavigationGestures = true |
22 | 67 |
|
| 68 | + // init observers |
| 69 | + webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: NSKeyValueObservingOptions.new, context: nil) |
| 70 | + webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: NSKeyValueObservingOptions.new, context: nil) |
| 71 | + } |
| 72 | + |
| 73 | + // Initialize UI |
| 74 | + func setupUI() { |
| 75 | + // UI elements |
| 76 | + leftButton.isEnabled = false |
| 77 | + progressBar = UIProgressView(frame: CGRect(x: 0, y: 0, width: webViewContainer.frame.width, height: 50)) |
| 78 | + progressBar.autoresizingMask = [.flexibleWidth] |
| 79 | + progressBar.progress = 0.0 |
| 80 | + progressBar.tintColor = UIColor.green.withAlphaComponent(0.6) |
| 81 | + webView.addSubview(progressBar) |
| 82 | + } |
| 83 | + |
| 84 | + // Initialize App and start loading |
| 85 | + func setupApp() { |
| 86 | + setupWebView() |
| 87 | + setupUI() |
23 | 88 |
|
| 89 | + // load startpage |
| 90 | + let urlRequest = URLRequest(url: appUrl!) |
| 91 | + webView.load(urlRequest) |
| 92 | + } |
| 93 | + |
| 94 | + // Cleanup |
| 95 | + deinit { |
| 96 | + webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.isLoading)) |
| 97 | + webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress)) |
| 98 | + } |
24 | 99 | } |
25 | 100 |
|
| 101 | +// WebView Event Listeners |
| 102 | +extension ViewController: WKNavigationDelegate { |
| 103 | + // didFinish |
| 104 | + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
| 105 | + // set title |
| 106 | + navigationItem.title = webView.title |
| 107 | + // hide progress bar |
| 108 | + progressBar.isHidden = true |
| 109 | + } |
| 110 | + // didFailProvisionalNavigation |
| 111 | + func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { |
| 112 | + // show offline screen |
| 113 | + // @TODO |
| 114 | + let alert = UIAlertController(title: "You're offline", message: "didFailProvisionalNavigation", preferredStyle: .alert) |
| 115 | + alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) |
| 116 | + present(alert, animated: true, completion: nil) |
| 117 | + } |
| 118 | + // didFail |
| 119 | + func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { |
| 120 | + // show offline screen |
| 121 | + // @TODO |
| 122 | + let alert = UIAlertController(title: "You're offline", message: "didFail", preferredStyle: .alert) |
| 123 | + alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) |
| 124 | + present(alert, animated: true, completion: nil) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// WebView |
| 129 | +extension ViewController: WKUIDelegate { |
| 130 | + // handle links opening in new tabs |
| 131 | + func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { |
| 132 | + if (navigationAction.targetFrame == nil) { |
| 133 | + webView.load(navigationAction.request) |
| 134 | + } |
| 135 | + return nil |
| 136 | + } |
| 137 | + // restrict navigation to target host, open external links in Safari |
| 138 | + func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { |
| 139 | + if let requestUrl = navigationAction.request.url { |
| 140 | + if let requestHost = requestUrl.host { |
| 141 | + if (requestHost.range(of: allowedOrigin) != nil ) { |
| 142 | + decisionHandler(.allow) |
| 143 | + } else { |
| 144 | + decisionHandler(.cancel) |
| 145 | + if (UIApplication.shared.canOpenURL(requestUrl)) { |
| 146 | + UIApplication.shared.open(requestUrl) |
| 147 | + } |
| 148 | + } |
| 149 | + } else { |
| 150 | + decisionHandler(.cancel) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments