GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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

3      

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.

3      

Thanks @fly0strich! You solved my problem.

3      

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

3      

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

3      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.