deleteCookie method

Future<void> deleteCookie({
  1. required Uri url,
  2. required String name,
  3. String path = "/",
  4. String? domain,
  5. InAppWebViewController? iosBelow11WebViewController,
})

Removes a cookie by its name for the given url, domain and path.

The default value of path is "/".

iosBelow11WebViewController is used for deleting the cookie (also session-only cookie) using JavaScript (cookie with isHttpOnly enabled cannot be deleted, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) from the current context of the WebView managed by that controller when you need to target iOS below 11. JavaScript must be enabled in order to work. In this case the url parameter is ignored.

NOTE for iOS below 11.0: If iosBelow11WebViewController is null or JavaScript is disabled for it, it will try to use a HeadlessInAppWebView to delete the cookie (session-only cookie and cookie with isHttpOnly enabled won't be deleted!).

Implementation

Future<void> deleteCookie(
    {required Uri url,
    required String name,
    String path = "/",
    String? domain,
    InAppWebViewController? iosBelow11WebViewController}) async {
  assert(url.toString().isNotEmpty);
  assert(name.isNotEmpty);

  if (await _shouldUseJavascript()) {
    await _setCookieWithJavaScript(
        url: url,
        name: name,
        value: "",
        path: path,
        domain: domain,
        maxAge: -1,
        webViewController: iosBelow11WebViewController);
    return;
  }

  Map<String, dynamic> args = <String, dynamic>{};
  args.putIfAbsent('url', () => url.toString());
  args.putIfAbsent('name', () => name);
  args.putIfAbsent('domain', () => domain);
  args.putIfAbsent('path', () => path);
  await _channel.invokeMethod('deleteCookie', args);
}