How to Enable Cookies in Android Webview

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?

How to programmatically accept cookies on Android Studio's WebView?

I figured it out after several hours:

CookieSyncManager.createInstance(webview.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();

cookieManager.setCookie(domain,"cookies-state=accepted");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.flush();
} else {
CookieSyncManager.getInstance().sync();
}

WebView not accepting some cookies

If you want to pass your login users data to a cookie in Webview the do it like this
The other day I had to pass my login object to particular URL
as follow.

            WebSettings settings = webViewRoi.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setAppCacheEnabled(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowFileAccessFromFileURLs(true);

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

FormsDTO formsDTO = new FormsDTO();
FormsDTOProfile dtoProfile = new FormsDTOProfile(LoginActivity.loginInfoDTO.getProfile());
formsDTO.setProfile(dtoProfile);
formsDTO.setAuthorized(true);
formsDTO.setToken(LoginActivity.loginInfoDTO.getToken());

String out123 = gson.toJson(formsDTO);

String auth2 = URLEncoder.encode(out123, "UTF-8");

String z = "userInfo=" + auth2; // here userinfo is the key of cookie
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);

cookieManager.setCookie(url, z);
webViewRoi.setWebChromeClient(new WebChromeClient());
webViewRoi.loadUrl(url);

So if you know the key name of cookie then you can pass your login object through cookie. hope it helps you.

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.

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.)



Related Topics



Leave a reply



Submit