Tuesday 28 December 2021

Android WebView of local website manipulating the URL

When using an Android WebView I am able to load in my custom web application using the view.loadUrl("https://example.com/assets/www/index.html") method.

However there is a slight issue. This will set the URL of my page to http://example.com/assets/www/index.html. What I would like to do, is to load my content using a much simpler URL such as: http://example.com

However I can't seem to find a solution for this other than hosting my website remotely.

Here is my current Activity:

class MainActivity : AppCompatActivity() {
    private var myWebView: WebView? = null

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val assetLoader = WebViewAssetLoader.Builder()
            .setDomain("example.com")
            .addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this))
            .addPathHandler("/build/", WebViewAssetLoader.AssetsPathHandler(this))
            .addPathHandler("/res/", WebViewAssetLoader.ResourcesPathHandler(this))
            .build()
        initiateWebView(findViewById(R.id.webv), assetLoader);
    }

    private fun initiateWebView(view: WebView, assetLoader: WebViewAssetLoader) {
        myWebView = view;

        view.webViewClient = LocalContentWebViewClient(assetLoader)
        view.settings?.javaScriptEnabled = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true)
        }
        myWebView?.addJavascriptInterface(JsWebInterface(this), "androidApp")
        view.loadUrl("https://example.com/assets/www/index.html")

    }

    override fun onBackPressed() {
        if (myWebView?.canGoBack() == true) {
            myWebView?.goBack()
        } else {
            super.onBackPressed()
        }
    }

}

private class LocalContentWebViewClient(private val assetLoader: WebViewAssetLoader) :
    WebViewClientCompat() {
    private val jsEventHandler = com.example.minsundhedpoc.JSEventHandler();

    @RequiresApi(21)
    override fun shouldInterceptRequest(
        view: WebView,
        request: WebResourceRequest
    ): WebResourceResponse? {
        return assetLoader.shouldInterceptRequest(request.url)
    }

    override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
        val url = view.url
        // Log.d("LOG","previous_url: " + url);
        return false
    }

    override fun onPageCommitVisible(view: WebView, url: String) {
        super.onPageCommitVisible(view, url)
        jsEventHandler.sendEvent(view, "myCustomEvent");

    }

    // to support API < 21
    override fun shouldInterceptRequest(
        view: WebView,
        url: String
    ): WebResourceResponse? {
        return assetLoader.shouldInterceptRequest(Uri.parse(url))
    }

}


from Android WebView of local website manipulating the URL

No comments:

Post a Comment