May I please have help in fixing these 5 errors in Xcode?

I was wondering if you would be able to help me with 5 errors that I am getting in the code that I am writing in Xcode using Swift. The 5 errors are cannot capture 'openPage' before it is declared, '(WKWebView, didFinishNavigation: WKNavigation!) → ()' does not have a member named 'loadRequest' (2 errors), cannot capture 'webView' before it is declared and '(WKWebView, didFinishNavigation: WKNavigation!) → ()' does not have a member named 'allowsBackForwardNavigationGestures'. I will attach the code below the question. Thanks if anyone can help.

Best Regards,

-Comm.Dan

import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {

    overridefunc viewDidLoad() {
        super.viewDidLoad()
        func openTapped() {
            let ac = UIAlertController(title: "Open page…", message: nil, preferredStyle: .ActionSheet)
            ac.addAction(UIAlertAction(title: "apple.com", style: .Default, handler: openPage))
            ac.addAction(UIAlertAction(title: "slashdot.org", style: .Default, handler: openPage))
            ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
            presentViewController(ac, animated: true, completion: nil)
        }
        func openPage(action: UIAlertAction!) {
            let url = NSURL(string: "http:/
            webView.loadRequest(NSURLRequest(URL: url))
        }
        func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
            title = webView.title
        }

      
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .Plain, target: self, action: "openTapped")
        let url = NSURL(string: "http:/
        webView.loadRequest(NSURLRequest(URL: url))
        webView.allowsBackForwardNavigationGestures = true
      

        /
    }
  
    var webView: WKWebView!
    overridefunc loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

    overridefunc didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }

}
Accepted Answer

First, your code has several functions declared inside of viewDidLoad() which should probably be declared in the scope of the class instead.


Move the openTapped(), openPage(action: UIAlertAction!), and webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) functions to within the class instead of within viewDidLoad().


Second, you should probably not be overriding loadView(). The code you have added there probably belongs in the viewDidLoad() method.


Once you get that stuff sorted out, post the updated code if you still have errors.

Dear LCS,

Thank you so much for the help. I really appreciate it.

Best Regards,

-Comm.Dan

Going over your code again, your override of the loadView() function is probably ok.

Swapping out the view like that is usually a last resort though. Can you set it in your storyboard?

Swapping out the view like that is usually a last resort though. Can you set it in your storyboard?

Alas, WKWebView can't be put in a nib (and hence can't be put in a storyboard). This is a known bug that's bitten me more than once )-:

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
May I please have help in fixing these 5 errors in Xcode?
 
 
Q