Skip to content

Commit 61be8f0

Browse files
committed
Add Day 25
1 parent d2b59a9 commit 61be8f0

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

Project4/Project4/ViewController.swift

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import WebKit
1111

1212
class ViewController: UIViewController, WKNavigationDelegate {
1313
var webView: WKWebView!
14+
var progressView: UIProgressView!
15+
var websites = ["apple.com", "hackingwithswift.com"]
16+
1417

1518
// gets called before viewDidLoad
1619
override func loadView() {
@@ -20,14 +23,17 @@ class ViewController: UIViewController, WKNavigationDelegate {
2023
}
2124

2225
func openPage(action: UIAlertAction) {
23-
let url = URL(string: "https://" + action.title!)!
26+
let url = URL(string: "https://" + websites[0])!
2427
webView.load(URLRequest(url: url))
2528
}
2629

2730
@objc func openTapped() {
2831
let ac = UIAlertController(title: "Open page…", message: nil, preferredStyle: .actionSheet)
29-
ac.addAction(UIAlertAction(title: "apple.com", style: .default, handler: openPage))
30-
ac.addAction(UIAlertAction(title: "hackingwithswift.com", style: .default, handler: openPage))
32+
33+
for website in websites {
34+
ac.addAction(UIAlertAction(title: website, style: .default, handler: openPage))
35+
}
36+
3137
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
3238
ac.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
3339
present(ac, animated: true)
@@ -37,17 +43,49 @@ class ViewController: UIViewController, WKNavigationDelegate {
3743
super.viewDidLoad()
3844
// Do any additional setup after loading the view.
3945
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .plain, target: self, action: #selector(openTapped))
46+
47+
progressView = UIProgressView(progressViewStyle: .default)
48+
progressView.sizeToFit()
49+
let progressButton = UIBarButtonItem(customView: progressView)
4050

51+
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
52+
let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
53+
54+
toolbarItems = [progressButton, spacer, refresh]
55+
navigationController?.isToolbarHidden = false
56+
4157

4258
let url = URL(string: "https://www.hackingwithswift.com")!
4359
webView.load(URLRequest(url: url))
4460
webView.allowsBackForwardNavigationGestures = true
61+
62+
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
63+
4564

4665
}
4766

4867
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
4968
title = webView.title
5069
}
51-
70+
71+
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
72+
if keyPath == "estimatedProgress" {
73+
progressView.progress = Float(webView.estimatedProgress)
74+
}
75+
}
76+
77+
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
78+
let url = navigationAction.request.url
79+
80+
if let host = url?.host {
81+
for website in websites {
82+
if host.contains(website) {
83+
decisionHandler(.allow)
84+
return
85+
}
86+
}
87+
}
88+
89+
decisionHandler(.cancel)
90+
}
5291
}
53-

0 commit comments

Comments
 (0)