@@ -11,6 +11,9 @@ import WebKit
11
11
12
12
class ViewController : UIViewController , WKNavigationDelegate {
13
13
var webView : WKWebView !
14
+ var progressView : UIProgressView !
15
+ var websites = [ " apple.com " , " hackingwithswift.com " ]
16
+
14
17
15
18
// gets called before viewDidLoad
16
19
override func loadView( ) {
@@ -20,14 +23,17 @@ class ViewController: UIViewController, WKNavigationDelegate {
20
23
}
21
24
22
25
func openPage( action: UIAlertAction ) {
23
- let url = URL ( string: " https:// " + action . title! ) !
26
+ let url = URL ( string: " https:// " + websites [ 0 ] ) !
24
27
webView. load ( URLRequest ( url: url) )
25
28
}
26
29
27
30
@objc func openTapped( ) {
28
31
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
+
31
37
ac. addAction ( UIAlertAction ( title: " Cancel " , style: . cancel) )
32
38
ac. popoverPresentationController? . barButtonItem = self . navigationItem. rightBarButtonItem
33
39
present ( ac, animated: true )
@@ -37,17 +43,49 @@ class ViewController: UIViewController, WKNavigationDelegate {
37
43
super. viewDidLoad ( )
38
44
// Do any additional setup after loading the view.
39
45
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)
40
50
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
+
41
57
42
58
let url = URL ( string: " https://www.hackingwithswift.com " ) !
43
59
webView. load ( URLRequest ( url: url) )
44
60
webView. allowsBackForwardNavigationGestures = true
61
+
62
+ webView. addObserver ( self , forKeyPath: #keyPath( WKWebView . estimatedProgress) , options: . new, context: nil )
63
+
45
64
46
65
}
47
66
48
67
func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
49
68
title = webView. title
50
69
}
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
+ }
52
91
}
53
-
0 commit comments