Niko
Niko

Reputation: 8153

CookieSyncManager sync asynchronous behavior

My question is pretty similar to Cookies & Webview - CookieSyncManager in Android!, but I really need to get the SystemClock.sleep() time adjusted or another way to get setCookie react immediately. Didn't work with 500ms, but worked with 5000ms, so it's not very reliable. Any way to loop sleep in onPageStarted where I call sync for CookieManager after setCookie?

My environment gets Cookie value from another server, so there is no session cookie here.

Thanks.

UPDATE:

Looks like Cookie handling is not working at all. I have following web flow:

  1. Set Cookie and load url
  2. Server handles that Cookie is okay, so it adds another cookie to indicate we have a session
  3. WebView still loads page showing no session
  4. I start my WebView again, and now getCookie shows the session Cookie and loads page properly

Upvotes: 0

Views: 722

Answers (2)

Niko
Niko

Reputation: 8153

I ended up calling setCookie before I even have WebView present in the application(I get the Cookie value from another server), also adding in WebViewClient onPageStarted and onPageFinishedthe cookie sync, it seems to be working and I didn't have to cause sleep delays.

Upvotes: 1

user2060386
user2060386

Reputation: 355

Before calling webview.loadUrl(url), I add a while loop to detect whether the cookie of the url has been injected. If the cookie hasn't been asynchronously set, sleep for some time. Hope it helps.

while (!CookieManager.getInstance().hasCookies() 
    || CookieManager.getInstance().getCookie(url)==null){
        SystemClock.sleep(TIME_WAITING_FOR_COOKIE_SYNC);
    }
webview.loadUrl(url);

Upvotes: 0

Related Questions