Webview and Cookies on Android

WebView and Cookies on Android

I figured out what's going on.

When I load a page through a server side action (a url visit), and view the html returned from that action inside a Webview, that first action/page runs inside that Webview. However, when you click on any link that are action commands in your web app, these actions start a new browser. That is why cookie info gets lost because the first cookie information you set for Webview is gone, we have a seperate program here.

You have to intercept clicks on Webview so that browsing never leaves the app, everything stays inside the same Webview.

  WebView webview = new WebView(this);      
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url); //this is controversial - see comments and other answers
return true;
}
});
setContentView(webview);
webview.loadUrl([MY URL]);

This fixes the problem.

Cookie doesn't work properly in webview in android

I had similar issue and I added the below code and worked.

String myURL = "https://www.yourWebPage.com";

android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();

cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();
cookieManager.setAcceptFileSchemeCookies(true);
cookieManager.getInstance().setAcceptCookie(true);
cookieManager.getCookie(myURL);

Hope it helps.

How to enable cookies in android webview?

CookieManager.getInstance() is the CookieManager instance for your entire application.
Hence, you enable or disable cookies for all the webviews in your application.

Normally it should work if your webview is already initialized:
http://developer.android.com/reference/android/webkit/CookieManager.html#getInstance()

Maybe you call CookieManager.getInstance().setAcceptCookie(true); before you initialize your webview and this is the problem?

Android WebView file:// urls with SameSite cookies

https://chromium.googlesource.com/chromium/src/+/1d127c933a4a39c65dc32cbd35bd511fd68ea452/android_webview/browser/cookie_manager.cc#317

// There are some unknowns about how to correctly handle file:// cookies,
// and our implementation for this is not robust. http://crbug.com/582985

It looks like the "best" way to load asset files in Android is not to use file:///android_asset/myfile.html, but to use a WebViewAssetLoader.

WebViewAssetLoader intercepts WebView requests, making all of your asset files appear on a fake HTTPS domain URL https://appassets.androidplatform.net/assets. Instead of file:///android_asset/myfile.html you'd load from https://appassets.androidplatform.net/assets/myfile.html.

The browser will treat that like a "real" HTTPS domain. SameSite=None will work normally, CORS will have a conventional non-null Origin, and there won't be any weirdness around sharing cookies between file:// URLs.

(But, even better than SameSite=None would be to use a fake subdomain of your actual domain. WebViewAssetLoader has a builder parameter allowing you to set the domain to a domain you control, e.g. if you own example.com, you could host assets on https://appassets.example.com, allowing you to share cookies with your website even with SameSite=Strict.)

I'm a beginner in Android web view development. What should I do to keep the cookies?

To keep cookies, you can use below:

CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);

You can extract all cookies from current url by this way from Webview as string:

@Override
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, "All the cookies in a string:" + cookies);
}


Related Topics



Leave a reply



Submit