-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
103 lines (77 loc) · 3.76 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// ViewController.swift
// SimpleBrowser
//
// Created by Julian Moorhouse on 10/07/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var progressView: UIProgressView!
var websites: [String]!
var selectedWebsiteIndex: Int!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .plain, target: self, action: #selector(openTapped))
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let back = UIBarButtonItem(title: "Back", style: .plain, target: webView, action: #selector(webView.goBack))
let forward = UIBarButtonItem(title: "Forward", style: .plain, target: webView, action: #selector(webView.goForward))
let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
progressView = UIProgressView(progressViewStyle: .default)
progressView.sizeToFit()
let progressButton = UIBarButtonItem(customView: progressView)
toolbarItems = [progressButton, spacer, back, forward, refresh]
navigationController?.isToolbarHidden = false
let url = URL(string: "https://" + websites[selectedWebsiteIndex])!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
}
@objc func openTapped() {
let ac = UIAlertController(title: "Open page...", message: nil, preferredStyle: .actionSheet)
for website in websites {
ac.addAction(UIAlertAction(title: website, style: .default, handler: openPage))
}
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
ac.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(ac, animated: true)
}
func openPage(action: UIAlertAction) {
guard let actionTitle = action.title else { return }
guard let url = URL(string: "https://" + actionTitle) else { return }
webView.load(URLRequest(url: url))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
progressView.progress = Float(webView.estimatedProgress)
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
let url = navigationAction.request.url
if let host = url?.host {
for website in websites {
if host.contains(website) {
decisionHandler(.allow)
return
}
}
}
decisionHandler(.cancel)
let link = url?.absoluteString
if !(link!.contains("about:blank")) {
let ac = UIAlertController(title: title, message: "Sorry this website is blocked!", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(ac, animated: true)
}
}
}