setCookie method
- required WebUri url,
- required String name,
- required String value,
- String path = "/",
- String? domain,
- int? expiresDate,
- int? maxAge,
- bool? isSecure,
- bool? isHttpOnly,
- HTTPCookieSameSitePolicy? sameSite,
- @Deprecated("Use webViewController instead") InAppWebViewController? iosBelow11WebViewController,
- InAppWebViewController? webViewController,
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.
The default value of path is "/".
The return value indicates whether the cookie was set successfully.
Officially Supported Platforms/Implementations:
- Android WebView (Official API - CookieManager.setCookie)
- iOS WKWebView (Official API - WKHTTPCookieStore.setCookie):
- On iOS below 11.0, the
webViewControllercould be used if you need to set a session-only cookie using JavaScript (soisHttpOnlycannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on the current URL of theWebViewmanaged by that controller. JavaScript must be enabled in order to work. IfwebViewControllerisnullor JavaScript is disabled for it, it will try to use a PlatformHeadlessInAppWebView to set the cookie (session-only cookie won't work! In that case, you should set alsoexpiresDateormaxAge). In this case, this method will return alwaystrue.
- On iOS below 11.0, the
- macOS WKWebView (Official API - WKHTTPCookieStore.setCookie):
- On macOS below 10.13, the
webViewControllercould be used if you need to set a session-only cookie using JavaScript (soisHttpOnlycannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on the current URL of theWebViewmanaged by that controller. JavaScript must be enabled in order to work. IfwebViewControllerisnullor JavaScript is disabled for it, it will try to use a PlatformHeadlessInAppWebView to set the cookie (session-only cookie won't work! In that case, you should set alsoexpiresDateormaxAge). In this case, this method will return alwaystrue.
- On macOS below 10.13, the
- Web <iframe> but requires same origin:
- The
webViewControllercould be used if you need to set a session-only cookie using JavaScript (soisHttpOnlycannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on the current URL of theWebViewmanaged by that controller. JavaScript must be enabled in order to work. IfwebViewControllerisnullor JavaScript is disabled for it, it will try to use a PlatformHeadlessInAppWebView to set the cookie (session-only cookie won't work! In that case, you should set alsoexpiresDateormaxAge). In this case, this method will return alwaystrue.
- The
- Windows WebView2:
- The
webViewControllercould be used to access cookies accessible only on the WebView managed by that controller, such as cookie with partition key.
- The
- Linux WPE WebKit (Official API - webkit_cookie_manager_add_cookie)
Parameters - Officially Supported Platforms/Implementations:
url: all platformsname: all platformsvalue: all platformspath: all platformsdomain: all platformsexpiresDate: all platformsmaxAge: all platformsisSecure: all platformsisHttpOnly: all platformssameSite: all platformswebViewController:- macOS WKWebView
- iOS WKWebView
- Web <iframe> but requires same origin
- Windows WebView2
Use the PlatformCookieManager.isMethodSupported method to check if this method is supported at runtime.
Implementation
Future<bool> setCookie({
required WebUri url,
required String name,
required String value,
String path = "/",
String? domain,
int? expiresDate,
int? maxAge,
bool? isSecure,
bool? isHttpOnly,
HTTPCookieSameSitePolicy? sameSite,
@Deprecated("Use webViewController instead")
InAppWebViewController? iosBelow11WebViewController,
InAppWebViewController? webViewController,
}) => platform.setCookie(
url: url,
name: name,
value: value,
path: path,
domain: domain,
expiresDate: expiresDate,
maxAge: maxAge,
isSecure: isSecure,
isHttpOnly: isHttpOnly,
sameSite: sameSite,
iosBelow11WebViewController: iosBelow11WebViewController?.platform,
webViewController: webViewController?.platform,
);