uiDelegate method create webview with configuration not called

2.7k Views Asked by At

I have assigned delegate to wkwebview uiDelegate and implemented it's method create web view with configuration, but it is not being called. I have even placed breakpoint but it is not hitting.

 class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.navigationDelegate = self
    webView.uiDelegate = self
    let requestURL = URL(string: "https://www.google.com")!
    let request = URLRequest(url: requestURL)
    webView.load(request as URLRequest)
}

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {

    let config = WKWebViewConfiguration()
    return WKWebView(frame: webView.frame, configuration: config)
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
}
1

There are 1 best solutions below

0
On

It seems like it doesnt call the delegate on the initial init of the webview ... I recommend you to just create the webview from code

    let configuration = WKWebViewConfiguration()
    configuration.allowsInlineMediaPlayback = true
    configuration.allowsAirPlayForMediaPlayback = true
    configuration.mediaTypesRequiringUserActionForPlayback = []

    webView = WKWebView(frame: containerView.frame, configuration: configuration)
    webView.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(webView)

    NSLayoutConstraint.activate([
        webView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
        webView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
        webView.topAnchor.constraint(equalTo: containerView.topAnchor),
        webView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
    ])

the containerview is just a basic UIView set in my xib so just set this uiview in your xib instead of a wkwebview and you should be good to go