0

I have a class that observes a user account. It starts observing it when the app starts. When the account changes, it sets the cookie in a cookie manager with setCookie.

When I start the app and the account is already there, it properly injects the cookie - when I open the web view I see it there.

When I start the app and the account is not there, nothing happens (that's expected), then I log in (this creates the account) and the code for injecting cookies is called (the callback of the setCookie says that the cookie was correctly injected). But when I open the web view, I do not see the cookie there. If I restart the app everything works again.

I tried many different configurations of web views and cookie manager. I tried call flush on a cookie manager. I tried setting acceptCookies, acceptThirdPartyCookies. On my web view I need to have javascript enabled and dom storage enabled.

This is how I set cookies:

fun injectCookie(account: Account) {
    val email = account.accountEmail ?: return
    val accessCode = account.accessCode ?: return
    val cookie = buildCookie(email, accessCode, DOMAIN)

    val manager = CookieManager.getInstance()
//    manager.setAcceptCookie(true)
    manager.setCookie(DOMAIN, cookie) { Log.d("COOKIE - auth", it.toString()) } // this prints true!
//    manager.flush() // I tried with and without flushing. I expect it to work without flush. 
}

I expected my cookies to be visible in web views immediately after the setCookie prints true but that's not happening in the cases when the user opens the app and the account is not there. Once the user logs in and the setCookie code is called it still doesn't work. But after restarting the app, everything works.

Almost like something at the beginning of the app prevents the app to set cookies. It works when I do it very early in the app lifecycle but not later.

Any ideas what could be the problem?

Markotron
  • 50
  • 8
  • Are you creating the webview before or after you get the cookie manager? Could it be using a different instance? – Gabe Sechan Apr 07 '19 at 03:23
  • I'm creating it after. Also CookieManager is a singleton, I'm getting it with `getInstance`. I tried creating a web view from different places in the app. – Markotron Apr 07 '19 at 13:06
  • I don't know what library you're using then. CookieManager in Android is NOT a signleton. It doesn't have a getInstance function. https://developer.android.com/reference/java/net/CookieManager – Gabe Sechan Apr 07 '19 at 14:42
  • You c/p a java.net.CookieManager. I'm using this: https://developer.android.com/reference/android/webkit/CookieManager – Markotron Apr 07 '19 at 15:35
  • Have you used the java.net.CookieManager with WebViews? If yes how do you specify to a webview to use a specific cookie manager? Do you have an example? Thanks for helping. – Markotron Apr 07 '19 at 15:49
  • Ok! Finally, I managed to solve the problem and of course, the solution is not android relevant but application relevant. I'm working on a big application with a lot of legacy code. Somewhere in the process of restoring data after login, we deleted cookies. The `CookieManager` behaves as expected. :) – Markotron Apr 11 '19 at 16:55

0 Answers0