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 the Chrome DevTools Protocol Network domain.
- On Web platform, it is implemented using Javascript.
Basic Usage
Note for Windows
If you are using a custom WebViewEnvironment
for your WebViews, be aware to set the webViewEnvironment
parameter when calling CookieManager.instance()
.
Note for iOS below 11.0, macOS 10.13 and Web platform (LIMITED SUPPORT!)
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");
Did you find it useful? Consider making a donation to support this project and leave a star on GitHub . Your support is really appreciated!