public class

CookieManager

extends Object
java.lang.Object
   ↳ com.tencent.smtt.sdk.CookieManager

Class Overview

Manages the cookies used by an application's WebView instances. Cookies are manipulated according to RFC2109.

Summary

Public Methods
boolean acceptCookie()
Gets whether the application's WebView instances send and accept cookies.
synchronized boolean acceptThirdPartyCookies(WebView w)
Gets whether the WebView should allow third party cookies to be set.
void flush()
Ensures all cookies currently accessible through the getCookie API are written to persistent storage.
String getCookie(String url)
Gets the cookies for the given URL.
static CookieManager getInstance()
Gets the singleton CookieManager instance.
boolean hasCookies()
Gets whether there are stored cookies.
void removeAllCookies(ValueCallback<Boolean> callback)
Removes all cookies.
void removeSessionCookies(ValueCallback<Boolean> callback)
Removes all session cookies, which are cookies without an expiration date.
synchronized void setAcceptCookie(boolean accept)
Sets whether the application's WebView instances should send and accept cookies.
synchronized void setAcceptThirdPartyCookies(WebView w, boolean accept)
Sets whether the WebView should allow third party cookies to be set.
synchronized void setCookie(String url, String value, boolean forceSet)
Sets a cookie for the given URL.
synchronized void setCookie(String url, String value, ValueCallback<Boolean> callback)
Sets a cookie for the given URL.
synchronized void setCookie(String url, String value)
Sets a cookie for the given URL.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public boolean acceptCookie ()

Gets whether the application's WebView instances send and accept cookies.

Returns
  • true if WebView instances send and accept cookies

public synchronized boolean acceptThirdPartyCookies (WebView w)

Gets whether the WebView should allow third party cookies to be set.

Parameters
w the WebView instance to get the cookie policy for
Returns
  • true if the WebView accepts third party cookies

public void flush ()

Ensures all cookies currently accessible through the getCookie API are written to persistent storage. This call will block the caller until it is done and may perform I/O.

public String getCookie (String url)

Gets the cookies for the given URL.

Parameters
url the URL for which the cookies are requested
Returns
  • value the cookies as a string, using the format of the 'Cookie' HTTP request header

public static CookieManager getInstance ()

Gets the singleton CookieManager instance.

Returns
  • the singleton CookieManager instance

public boolean hasCookies ()

Gets whether there are stored cookies.

Returns
  • true if there are stored cookies

public void removeAllCookies (ValueCallback<Boolean> callback)

Removes all cookies.

This method is asynchronous. If a ValueCallback is provided, onReceiveValue() will be called on the current thread's android.os.Looper once the operation is complete. The value provided to the callback indicates whether any cookies were removed. You can pass null as the callback if you don't need to know when the operation completes or whether any cookies were removed, and in this case it is safe to call the method from a thread without a Looper.

Parameters
callback a callback which is executed when the cookies have been removed

public void removeSessionCookies (ValueCallback<Boolean> callback)

Removes all session cookies, which are cookies without an expiration date.

This method is asynchronous. If a ValueCallback is provided, onReceiveValue() will be called on the current thread's android.os.Looper once the operation is complete. The value provided to the callback indicates whether any cookies were removed. You can pass null as the callback if you don't need to know when the operation completes or whether any cookie were removed, and in this case it is safe to call the method from a thread without a Looper.

Parameters
callback a callback which is executed when the session cookies have been removed

public synchronized void setAcceptCookie (boolean accept)

Sets whether the application's WebView instances should send and accept cookies. By default this is set to true and the WebView accepts cookies.

When this is true setAcceptThirdPartyCookies(WebView, boolean) setAcceptThirdPartyCookies can be used to control the policy for those specific types of cookie.

Parameters
accept whether WebView instances should send and accept cookies

public synchronized void setAcceptThirdPartyCookies (WebView w, boolean accept)

Sets whether the WebView should allow third party cookies to be set. Allowing third party cookies is a per WebView policy and can be set differently on different WebView instances.

Apps that target android.os.Build.VERSION_CODES#KITKAT or below default to allowing third party cookies. Apps targeting android.os.Build.VERSION_CODES#LOLLIPOP or later default to disallowing third party cookies.

Parameters
w the WebView instance to set the cookie policy on
accept whether the WebView instance should accept third party cookies

public synchronized void setCookie (String url, String value, boolean forceSet)

Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set will be ignored if it is expired.

Parameters
url the URL for which the cookie is to be set
value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header
forceSet true write immediately, otherwise not

public synchronized void setCookie (String url, String value, ValueCallback<Boolean> callback)

Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set will be ignored if it is expired.

This method is asynchronous. If a ValueCallback is provided, onReceiveValue(Object) will be called on the current thread's android.os.Looper once the operation is complete. The value provided to the callback indicates whether the cookie was set successfully. You can pass null as the callback if you don't need to know when the operation completes or whether it succeeded, and in this case it is safe to call the method from a thread without a Looper.

Parameters
url the URL for which the cookie is to be set
value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header
callback a callback to be executed when the cookie has been set

public synchronized void setCookie (String url, String value)

Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set will be ignored if it is expired.

Parameters
url the URL for which the cookie is to be set
value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header