TheFinestArtist / FinestWebView-Android

Beautiful and customizable Android Activity that shows web pages within an app.
https://finestwebview.web.app
2.32k stars 532 forks source link

Setting WebChromeClient and WebViewClient #162

Open ArabAgile opened 5 years ago

ArabAgile commented 5 years ago

I need to inject cookies and headers in each request. How possible it's using FinestWebView?

Also, I need to do something in shouldOverrideUrlLoading, how to achieve this?

Cililing commented 5 years ago

About Cookies - you can do it via CookieManager. For example:

fun setWebViewSession(url: String, onSessionSet: (() -> Unit)?) {
        doAsync {
            // Refresh login data
            ...
            // Build and execute request
            ...
            val response = httpClient.newCall(request).execute()

            // Obtain and sync cookies
            val cookies = response.headers().values("Set-Cookie")

            CookieSyncManager.createInstance(application)
            CookieSyncManager.getInstance().startSync()

            val cookieManager = CookieManager.getInstance()
            cookieManager.removeAllCookie()

            cookies.forEach {
                cookieManager.setCookie(URL(url).host, it)
            }

            CookieSyncManager.getInstance().sync()

            onSessionSet?.invoke()
        }
    }

This code snipped is about loggin user, that's why I make a request before setting cookies. For your purposes it should look fimilar.