3

Needed datas is not written to cookies immediately in WebView. But when I wait for 10-15 seconds everything is ok. To explain the situation, this example would be good as for me:

I open the app and login. After login, I close the app immediately. Then after I open the app again, it shows me to logout. But if I open the app after 1 minute, it shows again as logged in. For me cookies are written lately. But I cannot find solution. Please help me if you know.

I used CookieManager class but it doesn't help either.

CookieManager.getInstance().setAcceptCookie(true);
Johny
  • 625
  • 2
  • 6
  • 26
Vusala Hasanli
  • 409
  • 2
  • 7
  • 21

3 Answers3

6

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.

Johny
  • 625
  • 2
  • 6
  • 26
0

I had the similar problem, what I did is to get the cookies when login and set cookies for the load url you set on webView

        @Nullable
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            String resourceUrl = request.getUrl().toString();
            Log.e(MainActivity.TAG, "the request url :" + resourceUrl);
            CookieManager cookieManager1 = CookieManager.getInstance();
            // get the resourceUrl that has session
            if (resourceUrl.equals("the url has session")){
                String Cookies = cookieManager1.getCookie(resourceUrl);
                if (Cookies != null && Cookies.contains("sessionid")){
                    String[] cookiesList = Cookies.split(";");
                    cookieManager1.removeSessionCookies(null);
                    for (String c : cookiesList) {
                        cookieManager1.setCookie("the load url", c);
                    }
                }
            }
            return super.shouldInterceptRequest(view, request);

        }
0

This also happens with Webview Android from Flutter.

After logging in and using the app for more than 15 seconds then everything is ok, but if I kill the app before 15 seconds, the cookies can't be saved, and user will be considered a guest user.

I try to find a way to fix this in Webview Flutter but found no solution :(

By the way, my Webview using Oauth for login.

little_Friend
  • 198
  • 2
  • 14