B4A Library [lib] CookieManager

Here's a small library that enables you to manage any cookies used by a WebView in your application.

CookieManager
Version: 1.16
  • CookieManager
    Methods:
    • GetAcceptCookies As Boolean
      Returns True if cookies will be accepted by your application.
    • GetAllowFileSchemeCookies As Boolean
      Gets whether the application's WebView instances send and accept cookies for file scheme URLs.
    • GetCookie (Url As String) As String
      Returns the cookie for the given Url in the format: NAME=VALUE [; NAME=VALUE]
      If no cookie exists for the Url then a null value will be returned.
    • HasCookies As Boolean
      Returns True if any cookies are stored for your application.
    • RemoveAllCookies
      Removes all cookies stored for your application.
    • RemoveExpiredCookies
      Removes all expired cookies stored for your application.
    • RemoveSessionCookies
      Removes all session cookies stored for your application.
    • SetAcceptCookies (Accept As Boolean)
      Set whether cookies will be accepted for your application.
    • SetAcceptFileSchemeCookies (Accept As Boolean)
      Sets whether the application's WebView instances should send and accept cookies for file scheme URLs.
    • SetCookie (Url As String, Value As String)
      Sets the cookie for the Url to the Value.
  • CookieSyncManager
    Methods:
    • CreateInstance
      Creates an instance of the CookieSyncManager.
      The CookieSyncManager is used to synchronise the browser cookie store between RAM and permanent storage.
      To get the best performance, browser cookies are saved in RAM.
      A separate thread saves the cookies between, driven by a timer with a 5 minute interval.
    • ResetSync
      Resets the CookieSyncManager timer.
    • StartSync
      Requests the CookieSyncManager to start synchronisation.
      Typically called in Activity_Resume.
    • StopSync
      Requests the CookieSyncManager to stop synchronisation.
      Typically called in Activity_Pause.
    • Sync
      Forces the CookieSyncManager to synchronise now.
      This method is asynchronous, there is no guarantee it will synchronise immediately.

For an example of usage take a look here: http://www.b4x.com/forum/basic4andr...-load-url-username-password-2.html#post131419, and here: http://www.b4x.com/forum/basic4andr...-load-url-username-password-2.html#post134391.

Martin.
 

Attachments

  • CookieManager_v_1.16.zip
    17.4 KB · Views: 1,209
Last edited:

3fazeac

Member
Licensed User
Longtime User
Thank you. Yes, I spotted that later which explained why the local file system cookies were not working on 2.2.

However, since my javascript is local (hence the need for FileScheme cookies) I switched to your WebViewExtras library and that works very well on Android 2.2 as well as newer versions. I simply package up what would have been the cookies in to a string and use the CallSub() functions to send data back and forth.

So, thank you for that. A very useful library.
 

warwound

Expert
Licensed User
Longtime User
I don't think CookieManager will be of use to you.

You need to auto-accept whatever 'allow cookies' prompt a webpage displays.
That prompt might be an HTML <form> or it might be pure HTML.

The prompt at http://www.ad.nl/ad/nl/1012/Nederla...29/Technici-laten-dijk-expres-instorten.dhtml is not an HTML <form> it is an HTML <div> with an <a> anchor link that must be clicked to accept cookies:

B4X:
<a onclick="gaTrackEvent('cookieinfo_accepted', 'Ja, ik accepteer cookies');" class="btn-accept" href="/ad/acceptCookieCheck.do?url=http://www.ad.nl/ad/nl/1012/Nederland/article/detail/4152453/2015/09/29/Technici-laten-dijk-expres-instorten.dhtml"><span class="hidden">Ja, ik accepteer cookies</span></a>

The only solution i can think of is to execute some javascript, after the webpage has loaded, that will perform the 'allow cookies' action.

Have a read for more info about triggering a click on an anchor element: https://www.google.co.uk/search?q=javascript+trigger+anchor+click&ie=UTF-8&oe=UTF-8&gws_rd=ssl.

If the 'allow cookies' prompt is an HTML <form> then you'll find some examples on the forum that detail auto-completing and auto-submitting a form: https://www.b4x.com/android/forum/pages/results/?query=javascript+form&page=1&prefix=0.

As there is no standard method for a website to prompt the user to allow cookies you'll find each website uses it's own method.
So you will, i think, have to add code to your app for each website that you anticipate will be viewed - not an ideal solution.
 

warwound

Expert
Licensed User
Longtime User
Would it be possible to make (fake) this permission cookie? Is such a cookie unique for each user/device or generic?

I think it's possible to create such a cookie in b4a but how on earth would you know what key(s)/value(s) the cookie required?
If the cookie requires a value that is generated on the newpaper website's server then there no way you could guess what would be a valid value.

If i was you i think i'd clear my browser cache and cookies then visit some of these newspaper websites.
Click the 'accept cookies' prompt and then look in the browser's saved cookies for that website.
See if you can see what cookie was created after accepting the cookies and then look at the cookie value to see what key(s)/value(s) it has.

Maybe some (though ideally all) websites will create a cookie with key(s)/value(s) that you can create in b4a and avoid the cookie prompt.
 

enrique1

Member
Licensed User
Longtime User
Hello,

I'm having an issue regarding this topic and I've trying many differents methods to solve it without success.

Currently I'm using a webview to show an application located in my online server (mobilewebsite). Inside, Instagram login is needed. When I log in in an external web browser, the login is needed only once, later Instagram recognise the browser and provide the token without any problem for each request without log in again.

But, in the android webview, each time the user open the app, Instagram login is needed. It's like it was a new browser all times.

I've trying with the previous example, the Cookiemanager one, webviewextra, webviewsettings... but it doesn't work.

Any idea please??:(


Edit:
I've reading about 3rd party cookies in WebViews and it seem this is the main point. Could this be a solution?: http://developer.android.com/reference/android/webkit/CookieManager.html

B4X:
setAcceptThirdPartyCookies(WebView webview, boolean accept)
Sets whether the WebView should allow third party cookies to be set.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Hi.

Searching my hard drives for the source code for the CookieManager library i find it's gone missing!
It's been so long since i updated the library it has been discarded.

Anyway here's an easy way for you to try the CookieManager setAcceptThirdPartyCookies method.
First add this inline java to your activity:

B4X:
#If JAVA
import android.webkit.CookieManager;
import android.webkit.WebView;

public boolean GetAcceptThirdPartyCookies(WebView webview){
    return CookieManager.getInstance().getAcceptThirdPartyCookies(WebView webview);
}

public void SetAcceptThirdPartyCookies(WebView webview, boolean accept){
    CookieManager.getInstance().setAcceptThirdPartyCookies(WebView webview, boolean accept);
}
#End If

Now you can call either of those 2 methods using JavaObject, here's an example showing how to enable 3rd party cookies for 'MyWebView':

B4X:
Dim NativeMe As JavaObject
NativeMe.InitializeContext
NativeMe.RunMethod("SetAcceptThirdPartyCookies", Array As Object(MyWebView, True))
 

enrique1

Member
Licensed User
Longtime User
Hi warwound,

Thanks for your answer. Can you please tell me what I have to write in "NativeME.InitializeContext"?. I don't know which class I should use or whatever :S.

Thanks!

Hi.

Searching my hard drives for the source code for the CookieManager library i find it's gone missing!
It's been so long since i updated the library it has been discarded.

Anyway here's an easy way for you to try the CookieManager setAcceptThirdPartyCookies method.
First add this inline java to your activity:

B4X:
#If JAVA
import android.webkit.CookieManager;
import android.webkit.WebView;

public boolean GetAcceptThirdPartyCookies(WebView webview){
    return CookieManager.getInstance().getAcceptThirdPartyCookies(WebView webview);
}

public void SetAcceptThirdPartyCookies(WebView webview, boolean accept){
    CookieManager.getInstance().setAcceptThirdPartyCookies(WebView webview, boolean accept);
}
#End If

Now you can call either of those 2 methods using JavaObject, here's an example showing how to enable 3rd party cookies for 'MyWebView':

B4X:
Dim NativeMe As JavaObject
NativeMe.InitializeContext
NativeMe.RunMethod("SetAcceptThirdPartyCookies", Array As Object(MyWebView, True))
 

DonManfred

Expert
Licensed User
Longtime User
Top