Set a cookie to a webView in Android

前端 未结 6 1673
挽巷
挽巷 2020-12-05 07:13

I\'m getting a HttpResponse from a server when checking if a username or password is correct. When I load the url in a webview I want

6条回答
  •  Happy的楠姐
    2020-12-05 07:47

    Just wanna throw another way how this can be done. Not saying it is the best, but it is a way. You can use JavaScript to set cookies as well. Just override onPageFinished in WebViewClient

    new WebViewClient() {
    
            override fun onPageFinished(view: WebView, url: String) {
    
                    val javascript = """document.cookie = "key=$value""""
    
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        view.evaluateJavascript(javascript) { s -> Timber.d("Inject: %s", s) }
                    } else {
                        view.loadUrl("javascript:" + javascript, null)
                    }
            }
    }
    

    One thing with this approach: you will have to reload the webView. If anyone knows how to fix that, please comment.

提交回复
热议问题