Search code examples
iosswiftxcodewkwebview

Error : Cannot convert value of type 'URL?' to expected argument type 'URLRequest


import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    var webView : WKWebView!
     
    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let url = URL(string: "https://www.google.com")!
        webView.load(url)
        webView.allowsBackForwardNavigationGestures = true
           }
}

Hello, I am new to Swift programming. I am trying to load a simple website on navigation controller with the help of WKWebView. But I am unable to continue with it. As I am facing an error with line

let URL = let url = URL(string: "https://www.google.com")!

I need to know two things

  1. What does the error mean?
  2. How can I resolve it?

Solution

  • The error is pretty clear. You need to pass a URLRequest object as parameter.

    let url = URL(string: "https://www.google.com")!
    webView.load(URLRequest(url: url))