Cookie Manager
Supported Platforms: AndroidiOSmacOSWindowsWeb
To manage WebView
cookies, you can use the CookieManager
class, which implements a singleton object (shared instance).
On Android, it is implemented using the CookieManager class. On iOS and macOS, it is implemented using the WKHTTPCookieStore class. On Windows platform, it is implemented using Javascript. On Web platform, it is implemented using Javascript.
Basic Usage
If you are using a custom WebViewEnvironment
for your WebViews, be aware to set the webViewEnvironment
parameter when calling CookieManager.instance()
.
In this case, almost all the methods (CookieManager.deleteAllCookies
and CookieManager.getAllCookies
are not supported!) has been implemented using JavaScript because there is no other way to work with them on iOS below 11.0 and MacOS 10.13. Check here for JavaScript restrictions.
Example:
// get the CookieManager instance
CookieManager cookieManager = CookieManager.instance();
// set the expiration date for the cookie in milliseconds
final expiresDate = DateTime.now().add(Duration(days: 3)).millisecondsSinceEpoch;
final url = WebUri("https://flutter.dev/");
// set the cookie
await cookieManager.setCookie(
url: url,
name: "myCookie",
value: "myValue",
expiresDate: expiresDate,
isSecure: true,
);
// get cookies
List<Cookie> cookies = await cookieManager.getCookies(url: url);
// get a cookie
Cookie? cookie = await cookieManager.getCookie(url: url, name: "myCookie");
// delete a cookie
await cookieManager.deleteCookie(url: url, name: "myCookie");
// delete cookies
await cookieManager.deleteCookies(url: url, domain: ".flutter.dev");