Search code examples
androidfacebookwebviewandroid-webviewandroid-websettings

How to make webview work like Google Chrome for Facebook Links


I am creating an Android app, in which i am navigating Facebook Urls in webview.

Example Link: https://fb.watch/dk1YiCATL3/

If user is logged in via webview

Webview keeps converting short links like example link to

https://m.facebook.com/watch/?v= dk1YiCATL3&extid=CL-UNK-UNK-UNK-AN_GK0T-GK1C&ref=sharing

due to which link is not properly loaded and 'Content not found' shows up

enter image description here

Whereas if i try to enter the example link in Google Chrome it becomes a detailed link automatically and is loaded properly

https://www.facebook.com/FoodFusionPK/videos/756498368838785/?extid=CL-UNK-UNK-UNK-AN_GK0T-GK1C

What i tried:

  • I tried changing m.facebook inside url to www.facebook and then reload the link.
  • Tried to change the UserAgent for Desktop view.

What is required?

  • If user is logged in inside webview, and a public link like example link is loaded in webview it should laod like Google chrome instead of showing Content not found.

Below is the code part where i have added settings on Webview:

/*val DESKTOP_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"*/


var MOBILE_AGENT = "Mozilla/5.0 (Linux; Android 6.0.1; SM-J500M Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36"

  webView?.settings?.javaScriptEnabled = true
        webView?.settings?.loadWithOverviewMode = true
        webView?.settings?.setRenderPriority(WebSettings.RenderPriority.HIGH)
        webView?.settings?.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        webView?.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
        webView?.settings?.domStorageEnabled = true
        webView?.settings?.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webView?.settings?.useWideViewPort = true
        webView?.settings?.saveFormData = true
        webView?.settings?.setAppCacheEnabled(true)
        webView?.settings?.setEnableSmoothTransition(true)
        webView?.settings?.userAgentString = AppConstants.MOBILE_AGENT
        webView?.addJavascriptInterface(this, "mJava")
        webView?.settings?.javaScriptCanOpenWindowsAutomatically = true
        webView?.settings?.setSupportMultipleWindows(true)
        webView?.settings?.mediaPlaybackRequiresUserGesture = true

        webView.addJavascriptInterface(
            FacebookViewInterface(
                webView,
                sharedViewmodel
            ),
            "facebookDataGet",
        )

I am unable to figure out why the links are showing Content not found in Webview but same links are loaded properly in Google Chrome.

Can somebody please help me out with this

Any help will be appreciated.

Thank you


Solution

  • I am not seeing in your code a WebViewClient. Just try the following if that is the case:

        val myWebViewClient = object : WebViewClient() {
    
        }
    
        with(webview) {
            webViewClient = myWebViewClient
            settings.javaScriptEnabled = true
            loadUrl("https://fb.watch/dk1YiCATL3/")
        }