Hi.
Looks like CookieManager isn't supported on older API levels.
I originally tested the code i posted on an ICS device and it worked.
Testing again on the same ICS device fails, the activity crashes as soon as it loads logging: 'Fatal signal 11'.
WebView1.Initialize("WebView1")
' call SetAcceptCookie after WebView has been initialized
CookieManager1.SetAcceptCookies(True)
Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
Calling SetAcceptCookie after the WebView has been initialized fixes that crash and CookieManager works perfectly.
Now i've tested on a device with 2.3.7 and, as you posted, HasCookies always returns False.
I looked at the official CookieManager documentation:
CookieManager | Android Developers
It says CookieManager is supported by Android API level 1+, with the exception of the 2 FileSchemeCookies setter and getter methods which were added in API level 12.
There is another CookieManager class which is only available with API level 9+, java.net.CookieManager:
CookieManager | Android Developers but the B4A library doesn't use this java.net class.
I wondered whether HasCookies returns True only if there are cookies in saved storage and the method returns False even if there are cookies in memory but not saved storage.
So i created a new library object
CookieSyncManager, here's the updated library reference:
CookieManager
Comment: CookieManager is a simple wrapper for the Android <link>CookieManager|http://developer.android.com/reference/android/webkit/CookieManager.html</link> and <link>CookieSyncManager|http://developer.android.com/reference/android/webkit/CookieSyncManager.html</link> Classes.
It enables you to manages the cookies used by your application's WebView instances.
Author: Martin Pearman
Version: 1.1
- CookieManager
Methods:
- GetAcceptCookies As Boolean
Returns True if cookies will be accepted by your application.
- 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.
- SetCookie (Url As String, Value As String)
Sets the cookie for the Url to the Value.
- CookieSyncManager
Methods:
- Initialize
Initialize 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.
And example B4A code:
'Activity module
Sub Process_Globals
End Sub
Sub Globals
Dim CookieManager1 As CookieManager
Dim CookieSyncManager1 As CookieSyncManager
Dim WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
CookieSyncManager1.Initialize
WebView1.Initialize("WebView1")
Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
' call SetAcceptCookie after WebView has been initialized
CookieManager1.SetAcceptCookies(True)
' load a webpage that requires a login
Dim Url As String
Url="http://www.geograph.org.uk/profile/6526" ' change this to your login page
WebView1.LoadUrl(Url)
End Sub
Sub Activity_Resume
CookieSyncManager1.StartSync ' not really required for this test example
End Sub
Sub Activity_Pause (UserClosed As Boolean)
CookieSyncManager1.StopSync ' not really required for this test example
End Sub
Sub WebView1_PageFinished (Url As String)
' force the CookieSyncManager to synchronise
CookieSyncManager1.Sync
CookieSyncManager1.ResetSync
Log("WebView1_PageFinished Url = "&Url)
If CookieManager1.HasCookies Then
Log("Cookies: "&CookieManager1.GetCookie(Url))
Else
Log("No cookies found")
End If
End Sub
Still the Gingerbread device always logs 'No cookies found'.
The cookie does though seem to exist, if i edit the example:
Sub WebView1_PageFinished (Url As String)
' force the CookieSyncManager to synchronise
CookieSyncManager1.Sync
CookieSyncManager1.ResetSync
Log("WebView1_PageFinished Url = "&Url)
Log("Cookies: "&CookieManager1.GetCookie(Url))
End Sub
The log shows a cookie with a name of PHPSESSID after logging in to a website, before logging in the log reports 'Cookies: null'.
This behaviour is the same with and without the CookieSyncManager code, the CookieSyncManager seems to have no useful purpose here.
Can you test to see if GetCookie returns a value after logging in to a website and returns null if not logged in?
If might be that you'll have to test if GetCookie is Null or not instead of using the HasCookies method.
Martin.