Cookie manager is not deleting the cookies or expire them

20 Views Asked by At

I'm utilizing the android.webkit.CookieManager for managing cookies within my WebView. My process involves initially clearing out old cookies by setting their Max-Age=0, followed by introducing new cookies. The context of this operation is transitioning from a native screen to a WebView, during which I aim to ensure that any existing sessionId is replaced with my own, maintaining consistency across the navigation from the native screen to the WebView. Despite my efforts, I end up with two cookies bearing the same name but different values. How can I effectively delete or replace a specific cookie by its name?

class CookieProviderImpl(
    private val cookieManager: CookieManager,
    private val analyticsEventLogger: AnalyticsEventLogger,
) : CookieProvider {

    override suspend fun injectCookies() {
        val baseUrl = "www.baseURL.com"
        val currentCookies = cookieManager.getCookie(baseUrl)?.split("; ")?.associate {
            val (name, value) = it.split("=")
            name to value
        } ?: mapOf()

        cookieManager.setAcceptCookie(true)

        val cookiesToSet = mapOf(
            CookiesNames.DEVICE_ID to analyticsEventLogger.getVisitorId(),
            CookiesNames.SESSION_ID to analyticsEventLogger.getSessionId(),
            CookiesNames.SESSION_TIMESTAMP to System.currentTimeMillis().toString()
        )

        currentCookies.forEach { (name, _) ->
            if (cookiesToSet.keys.contains(name)) {
                cookieManager.setCookie(baseUrl, "$name=''; Max-Age=0")
            }
        }

        cookieManager.flush()
        cookiesToSet.forEach { (name, value) ->
            cookieManager.setCookie(baseUrl, "$name=$value")
        }

        cookieManager.flush()
    }

    override fun removeAllCookies() {
        cookieManager.removeAllCookies(null)
    }
}

The architecture is CookieProvider is injected in ViewModel, where VM is injected in composable function where I am using AndroidView function to load the page

I inject the cookies in two places, first on initialization

LaunchedEffect(key1 = Unit, block = {
    launch(Dispatchers.Main) {
        viewModel.injectCookies()
    }
})

And after that every time before loading new URL

AndroidView(
        factory = { context ->
            WebView(context).apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                
                webViewClient = object : WebViewClient() {
                    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                        onCanGoBackChange(view?.canGoBack() ?: false)
                        super.onPageStarted(view, url, favicon)
                    }

                    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                        runBlocking { injectCookies() }
                        return super.shouldOverrideUrlLoading(view, request)
                    }
                }
              
            }
        },
        update = { webview ->
            onWebViewUpdate(webview)
            webview.loadUrl(url)
        }
    )
0

There are 0 best solutions below