Provides methods for getting, setting, and deleting HTTP cookies. You can access the ICookieManager via Web.CookieManager.
When developing code that interacts with cookies, it may also be helpful to view a webview's cookies using remote debugging.
Task<bool> DeleteCookies(string url, string cookieName = null)
Deletes all of the cookies that match the given URL and returns a
Task<bool> indicating whether the deletion succeeded. A cookieName
can be optionally passed as a second parameter to further filter
to a specific cookie.
If a deletion fails, it could be because the URL was invalid.
For more details regarding a failure, check the Unity logs.
if (Web.CookieManager == null) {
Debug.Log("Web.CookieManager isn't supported on this platform.");
return;
}
// Delete all the cookies for this cookie test page, which will reset the test.
var succeeded = await Web.CookieManager.DeleteCookies("http://www.whatarecookies.com/cookietest.asp");
Debug.Log("Cookie deletion succeeded: " + succeeded);
On Windows and macOS, if this method is called without a cookieName
it only deletes cookies that were set without an explicit Domain attribute.
On versions of iOS older than iOS 11, session cookies are excluded because WKHTTPCookieStore is only supported in iOS 11 and newer.
Task<Cookie[]> GetCookies(string url, string cookieName = null)
Gets all of the cookies that match the given URL. A cookieName
can be optionally passed as a second parameter to further filter results to a specific cookie.
if (Web.CookieManager == null) {
Debug.Log("Web.CookieManager isn't supported on this platform.");
return;
}
// Get the cookie named "NID" set by google.com.
var cookies = await Web.CookieManager.GetCookies("https://www.google.com", "NID");
if (cookies.Length > 0) {
Debug.Log("Cookie: " + cookies[0]);
} else {
Debug.Log("Cookie not found.");
}
On Android, the cookies returned only have their Name and Value fields set. The other fields (e.g. Domain, Path) are set to their default values because Android doesn't provide a way to access those values.
On versions of iOS older than iOS 11, session cookies are excluded because WKHTTPCookieStore is only supported in iOS 11 and newer.
Task<bool> SetCookie(Cookie cookie)
Sets the given cookie and returns a Task<bool> indicating whether the cookie was set successfully. If setting the cookie fails, it could be because the data in the provided Cookie was malformed. For more details regarding a failure, please check the Unity logs.
if (Web.CookieManager == null) {
Debug.Log("Web.CookieManager isn't supported on this platform.");
return;
}
var success = await Web.CookieManager.SetCookie(new Cookie {
Domain = "vuplex.com",
Path = "/",
Name = "example_name",
Value = "example_value",
Secure = true,
// Expire one day from now
ExpirationDate = (int)DateTimeOffset.Now.ToUnixTimeSeconds() + 60 * 60 * 24
});