B4A Library [lib] CookieManager

3fazeac

Member
Licensed 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.
 

Rick Harris

Well-Known Member
Licensed User
Hello Martin,

I am trying to use your cookiemanager to give permission to place cookies by means of software.
Dutch newspapers all need cookie permission nowadays. Is there a way to use your library to give cookie permission, like in this example: http://www.ad.nl/ad/nl/1012/Nederla...29/Technici-laten-dijk-expres-instorten.dhtml
I want my software to press the permission button, because my blind clients cannot find these permission buttons.
 

warwound

Expert
Licensed 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: http://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.
 

Rick Harris

Well-Known Member
Licensed 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: http://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.
Thank you very much for this detailed explanation. Yes a separate solution for each newspaper is needed. A "no cookies thanks" software for Windows already exists.

I assume these newspapers place a cookie that indicate that the user has clicked on the "accept" button in order for the permission window not to appear the next time. Would it be possible to make (fake) this permission cookie? Is such a cookie unique for each user/device or generic?

Governments do stupid things. Two years ago the so called "cookie wall" law was relaxed here in Holland, but now some government agency has threatened the newspapers with a fine if they don't explicitly ask permission to place tracking cookies. For my blind users this cookie-wall is a nightmare. Furthermore, the government seems to be ignorent about the (far worse) trackers that are placed in their thousands on websites...
 
Last edited:

warwound

Expert
Licensed 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
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
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
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))
 
Top