Webview Cookie Manager

pub package

A flutter library to manager your web cookies for Android (API level 9+) and iOS (11+).

The cookies stores and retrieves using the httpCookieStore for iOS and CookieManager for Android.

Get started iOS

This plugin supports both CocoaPods and Flutter's Swift Package Manager integration. Both use the same cookie implementation and require no changes to your Dart code.

The package requires Flutter 3.29.0 or newer. CocoaPods remains supported for applications that have not enabled Swift Package Manager. The plugin's minimum iOS deployment target remains 11.0; your Flutter SDK or other dependencies may require a higher app deployment target.

For Swift Package Manager, use Flutter 3.44 or newer and follow the Flutter app migration guide. Use flutter config --enable-swift-package-manager to enable it, then rebuild your app with Flutter. Older Flutter versions should use CocoaPods. Use flutter config --no-enable-swift-package-manager to keep using CocoaPods. This is a Flutter plugin, not a standalone Swift package for non-Flutter apps.

Usage

The WebCookieManager can be used directly or together with webview_flutter.

Get cookies

final cookieManager = WebviewCookieManager();

final gotCookies = await cookieManager.getCookies('https://youtube.com');
for (var item in gotCookies) {
  print(item);
}
await cookieManager.setCookies([
              Cookie('cookieName', 'cookieValue')
                ..domain = 'youtube.com'
                ..expires = DateTime.now().add(Duration(days: 10))
                ..httpOnly = false
            ]);
await cookieManager.hasCookies();
await cookieManager.removeCookie('https://youtube.com');

Clear cookies

await cookieManager.clearCookies();

Domain attribute

Domain attribute is not required according to RFC, but it is important to remember that empty domain causes undefined behavior. So it is highly reccommended to specify it this this way:

final cookie = Cookie('cookieName', 'cookieValue')..domain = 'youtube.com';

Secure attribute

If you see the error Strict Secure Cookie policy does not allow setting a secure cookie for http://your-domain.net/ for apps targeting >= R. Please either use the 'https:' scheme for this URL or omit the 'Secure' directive in the cookie value. Then you need to set the origin while setting the cookie, in that case setting the domain is not required.

final cookies = <Cookie>[];
// Add your cookie with secure flag to the array
cookieManager.setCookies(cookies, origin: 'https://your-domain.net')

Troubleshooting on iOS

  1. Set minimum target iOS version to 11 (see also #17)
  2. If you are using Objective C, check that PodFile have a flag use_frameworks (see also #4)
target 'Runner' do
  use_frameworks!
  use_modular_headers!
  ..........
end

Notes

This is a fork from web_cookie_manager since is a package without maintenance. This is compatible with Flutter 3.29.0