setCookie method

  1. @SupportedPlatforms.new(platforms: [AndroidPlatform(apiName: 'CookieManager.setCookie', apiUrl: 'https://developer.android.com/reference/android/webkit/CookieManager#setCookie(java.lang.String,%20java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.Boolean%3E)'), IOSPlatform(apiName: 'WKHTTPCookieStore.setCookie', apiUrl: 'https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie', note: """On iOS below 11.0, the [webViewController] could be used if you need to set a session-only cookie using JavaScript (so [isHttpOnly] cannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on the current URL of the `WebView` managed by that controller. JavaScript must be enabled in order to work. If [webViewController] is `null` or 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 also [expiresDate] or [maxAge]). In this case, this method will return always `true`."""), MacOSPlatform(apiName: 'WKHTTPCookieStore.setCookie', apiUrl: 'https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie', note: """On macOS below 10.13, the [webViewController] could be used if you need to set a session-only cookie using JavaScript (so [isHttpOnly] cannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on the current URL of the `WebView` managed by that controller. JavaScript must be enabled in order to work. If [webViewController] is `null` or 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 also [expiresDate] or [maxAge]). In this case, this method will return always `true`."""), WebPlatform(note: """The [webViewController] could be used if you need to set a session-only cookie using JavaScript (so [isHttpOnly] cannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on the current URL of the `WebView` managed by that controller. JavaScript must be enabled in order to work. If [webViewController] is `null` or 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 also [expiresDate] or [maxAge]). In this case, this method will return always `true`."""), WindowsPlatform(note: 'The [webViewController] could be used to access cookies accessible only on the WebView managed by that controller, such as cookie with partition key.'), LinuxPlatform(apiName: 'webkit_cookie_manager_add_cookie', apiUrl: 'https://wpewebkit.org/reference/stable/wpe-webkit-2.0/method.CookieManager.add_cookie.html')])
Future<bool> setCookie({
  1. required WebUri url,
  2. required String name,
  3. required String value,
  4. String path = "/",
  5. String? domain,
  6. int? expiresDate,
  7. int? maxAge,
  8. bool? isSecure,
  9. bool? isHttpOnly,
  10. HTTPCookieSameSitePolicy? sameSite,
  11. @Deprecated("Use webViewController instead") PlatformInAppWebViewController? iosBelow11WebViewController,
  12. @SupportedPlatforms.new(platforms: [MacOSPlatform(), IOSPlatform(), WebPlatform(), WindowsPlatform()]) PlatformInAppWebViewController? 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:

Parameters - Officially Supported Platforms/Implementations:

  • url: all platforms
  • name: all platforms
  • value: all platforms
  • path: all platforms
  • domain: all platforms
  • expiresDate: all platforms
  • maxAge: all platforms
  • isSecure: all platforms
  • isHttpOnly: all platforms
  • sameSite: all platforms
  • webViewController:
    • 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

@SupportedPlatforms(
  platforms: [
    AndroidPlatform(
      apiName: 'CookieManager.setCookie',
      apiUrl:
          'https://developer.android.com/reference/android/webkit/CookieManager#setCookie(java.lang.String,%20java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.Boolean%3E)',
    ),
    IOSPlatform(
      apiName: 'WKHTTPCookieStore.setCookie',
      apiUrl:
          'https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie',
      note:
          """On iOS below 11.0, the [webViewController] could be used if you need to set a session-only cookie using JavaScript
(so [isHttpOnly] cannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on
the current URL of the `WebView` managed by that controller. JavaScript must be enabled in order to work.
If [webViewController] is `null` or 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 also [expiresDate] or [maxAge]).
In this case, this method will return always `true`.""",
    ),
    MacOSPlatform(
      apiName: 'WKHTTPCookieStore.setCookie',
      apiUrl:
          'https://developer.apple.com/documentation/webkit/wkhttpcookiestore/2882007-setcookie',
      note:
          """On macOS below 10.13, the [webViewController] could be used if you need to set a session-only cookie using JavaScript
(so [isHttpOnly] cannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on
the current URL of the `WebView` managed by that controller. JavaScript must be enabled in order to work.
If [webViewController] is `null` or 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 also [expiresDate] or [maxAge]).
In this case, this method will return always `true`.""",
    ),
    WebPlatform(
      note:
          """The [webViewController] could be used if you need to set a session-only cookie using JavaScript
(so [isHttpOnly] cannot be set, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) on
the current URL of the `WebView` managed by that controller. JavaScript must be enabled in order to work.
If [webViewController] is `null` or 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 also [expiresDate] or [maxAge]).
In this case, this method will return always `true`.""",
    ),
    WindowsPlatform(
      note:
          'The [webViewController] could be used to access cookies accessible only on the WebView managed by that controller, such as cookie with partition key.',
    ),
    LinuxPlatform(
      apiName: 'webkit_cookie_manager_add_cookie',
      apiUrl:
          'https://wpewebkit.org/reference/stable/wpe-webkit-2.0/method.CookieManager.add_cookie.html',
    ),
  ],
)
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")
  PlatformInAppWebViewController? iosBelow11WebViewController,
  @SupportedPlatforms(
    platforms: [
      MacOSPlatform(),
      IOSPlatform(),
      WebPlatform(),
      WindowsPlatform(),
    ],
  )
  PlatformInAppWebViewController? webViewController,
}) {
  throw UnimplementedError(
    'setCookie is not implemented on the current platform',
  );
}