WebViewCookieTools Extension


Clear cookies in App Inventor and Thunkable

This extension comes via a request by community member giuseppe, who is building an application inside of a WebView in App Inventor and required the ability to clear cookies from the WebView to clear out any remaining login details to completely log a user out of his application.

A cookie is a piece of temporary data stored in your browser by a website, this data could be anything from the items in your shopping cart, to your access token for a website.

This was a simple enough extension to create and so I present the WebViewCookieTools extension, the methods available in this extension enable you to set cookies, clear all cookies, and get any cookies that are set.

Note if you use multiple WebViews in your app this will clear the cookies from all of them because of the way cookies work in Android you modify cookies based on the app context rather than by element.

ClearCookies

Clears all cookies from the current app context. This method is backwards compatible with Android versions prior to API level 21(Android Lollypop) however the callback "CookiesRemoved" will not fire on earlier versions.

Returns Nothing


HasCookies

Returns true if there are cookies in the current context, returns false if there are no cookies set.

Returns Boolean(True or False)


GetCookies

Returns a String with the cookies from the specified URL, if there are no cookies this method returns an empty string.

Returns String


SetCookie

Sets a cookie for the specified URL in the current context.

Returns Nothing



CookiesRemoved

This event fires when cookies have been removed from the current context. Note this event will not fire for versions of Android prior to API level 21(Android Lollypop). The boolean value isCookiesRemoved relates to whether cookies were removed, if there are no cookies set this returns false.

CookieSet

This event fires when a cookie has been set, the boolean value isCookieSet relates to whether the cookie was set successfully.


Example

In the following example I have a button for testing each method and a webview on Google.com, Google sets cookies when you load the page so as soon as the WebView loads the pages there are cookies present.


Here you can see the complete code in this example and how easy it is to use in your apps.







Version History

Version 2

-Added event "CookiesRemoved" with a value "isCookiesRemoved"
-Added method "GetCookies"
-Added method "SetCookie"
-Added event "CookieSet" with a value "isCookieSet"

Version 1

-Added "HasCookies" method
-Added "ClearCookies" method






Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY

Was this helpful?

Yes No

Comments

Thunkable Components