ViewController.swift
easyWebBrowswerProject4
Created by on 12/21/22.
import UIKit
import WebKit
"""
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var progressView: UIProgressView!
var websites = ["apple.com", "hackingwithswift.com"]
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
//creates a new url out of the string hackingwithswift.com
let url = URL(string: "https://" + websites[0])!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
//we are going to lock this app down so it selects websites selected by our user and that means giving our
//users and option of some sites and that means adding a button to the navigation bar
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "OPEN...", style: .plain,target: self, action: #selector(buttonTapped))
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
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, refresh]
navigationController?.isToolbarHidden = false
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
// Do any additional setup after loading the view.
}
@objc func buttonTapped() {
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: "hackingwithswift.com", style: .default, handler: openPage))
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel))
ac.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(ac, animated: true)
}
func openPage(action: UIAlertAction){
let url = URL(string: "https://" + action.title!)!
webView.load(URLRequest(url: url))
}
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, didFinish navigation: WKNavigation!) {
title = webView.title
}
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)
}
}
}
decisionHandler(.cancel)
}
}
"""
I keep getting an error that says this:
"""
Thread 1: Completion handler passed to -[easyWebBrowswerProject4.ViewController webView:decidePolicyForNavigationAction:decisionHandler:] was called more than once
"""
The thing is if I delete and rewrite that last webView function, the project runs just fine but just for one run, and then throws the same error again. What is going on?