Webview cannot accept cookies
Asked Answered
C

5

15

I'm creating a WebView based Android app that enables the user to login onto a mobile operator. When I run the app the WebView opens the website but I get a message that the WebView doesn't allow cookies. I've tried various codes that I found here but none of them worked. Can anyone help me? Here is the code I'm using:

//in oncreate
final CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
final CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
String[] cookies = getCookie("https://myaccount.ee.co.uk/login-dispatch/?fa=register");
for (String cookie : cookies) {
    cookieManager.setCookie("https://myaccount.ee.co.uk/login-dispatch/?fa=register", cookie);
}
cookieSyncManager.sync();
webView.loadUrl("https://myaccount.ee.co.uk/login-dispatch/?fa=register");

and the getCookies method:

public String[] getCookie(String siteName) {
     CookieManager cookieManager = CookieManager.getInstance();
     String cookies = cookieManager.getCookie(siteName);
     String[] cookiesArray = cookies.split(";");
     return cookiesArray;
}
Crowson answered 30/11, 2015 at 12:27 Comment(2)
What is the exact message your are getting about the webview not allowing cookies? Are you seeing that as an alert on the UI or a message in logcat, or somewhere else?Sundae
@Sundae im getting a message from their website that cookies are not availableCrowson
T
8

Try to override the shouldOverrideUrlLoading method to make your WebView handle all URL links, and not start a new browser on any link.

webview.setWebViewClient(new WebViewClient() {  
          @Override  
          public boolean shouldOverrideUrlLoading(WebView view, String url)  
          {  
            return false;  
          }  
});   

webview.loadUrl(url);

If this doesn't work you can replace your

cookieManager.setAcceptCookie(true);

to

cookieManager.setAcceptThirdPartyCookies(true);

because cookieManager.setAcceptCookie(true); doesn't work on Lollipop.

Truncation answered 8/12, 2015 at 2:23 Comment(0)
C
3

Have you checked Android documentation ?

If you are using API level >= 21, WebView automatically syncs cookies.

If not try this:

    final CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    final CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    cookieManager.setAcceptCookie(true);
    String[] cookies = getCookie(cookieManager, "https://myaccount.ee.co.uk/login-dispatch/?fa=register");
    for (String cookie : cookies) {
        cookieManager.setCookie("https://myaccount.ee.co.uk/login-dispatch/?fa=register", cookie);
    }
    cookieSyncManager.sync();
    webView.loadUrl("https://myaccount.ee.co.uk/login-dispatch/?fa=register");

getCookies:

  public String[] getCookie(CookieManager cookieManager, String siteName){
        String cookies = cookieManager.getCookie(siteName);
        String[] cookiesArray=cookies.split(";");
        return cookiesArray;
    }
Centric answered 4/12, 2015 at 8:17 Comment(1)
Could you give it a try by changing setAcceptCookie to setAcceptThirdPartyCookies(webView, true)Centric
A
3

You have to enable javascript and then getting instance of cookie manager accept cookie By using javascriptenable cookie gets stored

webView.getSettings().setJavaScriptEnabled(true); CookieManager.getInstance().setAcceptCookie(true);

Apriorism answered 9/12, 2015 at 6:48 Comment(0)
S
0

Oftentimes login requires javascript, try adding this before loadUrl:

webView.getSettings().setJavaScriptEnabled(true);

Sundaysundberg answered 9/12, 2015 at 0:5 Comment(0)
S
0

@Darko.

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.

Thanks,

Sulfaguanidine answered 9/12, 2015 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.