`$cookies.put()` takes 30 seconds to save the cookie on disk on Android

你说的曾经没有我的故事 提交于 2021-02-18 05:35:28

问题


Context:

  • We have a cordova application loading an online website. (We basically use cordova for the plugins)
  • We store a cookie using $cookies.put()
  • It works fine in desktop browsers and on iOS (as long we use expiration on the cookie)

Problem:

On Android if the user kill the application with 30 seconds after $cookies.put() is called the cookie will be gone on the next reboot.

If the user wait more than 30 seconds (like 35 seconds or more) everything works fine.

Question:

How can we force the cookie to be saved instantly (or at least much faster)?

Notes:

  • Adding options on the cookie does not change anything
  • I tried to replace the system browser with crosswalk using cordova-plugin-crosswalk-webview but it does the same thing

回答1:


I had the same problem and made a small plugin (still work in progress) to fix that.

It exposes the flush method, which will apply your cookie modifications (put and remove) to persistent memory.

link to cordova-plugin-cookie-manager

Feel free to contribute.




回答2:


The CookieManager design in the android makes it difficult to implement. The webview and the cookiemanager which contain their own in_memory databases and are in sync to one another hence there is no need to sync the two of them.

The removeSessionCookie doesn't provide a callback or any way for it to complete and since this method has been implemented in the WebView classic versions.

Here are some solutions in which you can sync up between HttpUrlCookieManager and the android webkit cookie manager

Sync up link 1

Another thing to note here is when you use $cookie.put() it defines the cookies to be sent along with the rest of the HTTP headers to the client but if you use the third party api's to get the cookies it may not contain cookies only from the client anymore which may result in unwanted side effects.

You may refer to the link here as suggested by Author to this post - author' post and here is the link for the protocols to be used while defining the cookies link here

Here is another link that i stumbled upon which may help a bit

link here

or to use the same repository/database of the webview and the HttpUrlConnection you can create your own handler as implemented in the following stack thread Stack thread

Hope it may help you a bit.



来源:https://stackoverflow.com/questions/39842621/cookies-put-takes-30-seconds-to-save-the-cookie-on-disk-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!