Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView shows google search result as raw html

I am trying to add a web browser in an app that I'm working on using iOS WKWebView.

But the WKWebView loads the raw html for google search result. https://i.sstatic.net/lx2cD.jpg

Here's a part of the code I'm using:

let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true

webView = WKWebView(frame: CGRect.zero, configuration: configuration)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
webView.navigationDelegate = self
webView.UIDelegate = self
webviewHolder.addSubview(webView)

webView.scrollView.backgroundColor = UIColor.clearColor()
webView.backgroundColor = UIColor.clearColor()
webView.opaque = false
webView.clipsToBounds = false
webView.scrollView.clipsToBounds = false

and for the WKNavigationDelegate and WKUIDelegate implementations:

//MARK: WKNavigationDelegate
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    pageLoaded = true
    updateBarButtons()
    addressBarTextField.text = webView.URL?.absoluteString
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}

func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
    cantConnectView.hidden = false
    webView.hidden = true
}

func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) {
    cantConnectView.hidden = false
    webView.hidden = true
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}

func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    updateBarButtons()
    addressBarTextField.text = webView.URL?.absoluteString
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    decisionHandler(.Allow)
}

func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {
    decisionHandler(.Allow)
}

//MARK: WKUIDelegate
func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    if navigationAction.targetFrame == nil {
        webView.loadRequest(navigationAction.request)
    }

    return nil
}

Any ideas to make WKWebView load google search correctly?

like image 683
Marlo Quidato Avatar asked Dec 01 '25 00:12

Marlo Quidato


1 Answers

Fixed it by using Safari's user agent

if #available(iOS 9.0, *) {
    webView.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13D15 Safari/601.1"
}
like image 109
Marlo Quidato Avatar answered Dec 02 '25 13:12

Marlo Quidato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!