UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Project 4 compiler error driving me nuts!

Forums > 100 Days of Swift

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() {

    //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)
    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) {






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?


You need to put a return immediately after the decisionHandler(.allow) line. If you don't return from the function after that line, then it continues on to the line where decisionHandler(.cancel) is called. So, two different decisionHandler() are being called.


Thanks @fly0strich! You solved my problem.


just adding the return statement did not solve my issue please help, after adding it still crashes and goes into main.m autoreleasepool


Please share the code, so that others can look into it.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.