deleteCookies method

Future<void> deleteCookies({
  1. required WebUri url,
  2. String path = "/",
  3. String? domain,
  4. @Deprecated("Use webViewController instead") InAppWebViewController? iosBelow11WebViewController,
  5. InAppWebViewController? webViewController,
})

Removes all cookies for the given url, domain and path.

The default value of path is "/".

webViewController is used for deleting the cookies (also session-only cookies) using JavaScript (cookies 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, MacOS below 10.13 and Web platform. JavaScript must be enabled in order to work. In this case the url parameter is ignored.

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

NOTE for Web: this method will have effect only if the iframe has the same origin. If webViewController is null or JavaScript is disabled for it, it will try to use a HeadlessInAppWebView to delete the cookies (session-only cookies and cookies with isHttpOnly enabled won't be deleted!).

Supported Platforms/Implementations:

  • Android native WebView
  • iOS
  • MacOS
  • Web

Implementation

Future<void> deleteCookies(
    {required WebUri url,
    String path = "/",
    String? domain,
    @Deprecated("Use webViewController instead")
    InAppWebViewController? iosBelow11WebViewController,
    InAppWebViewController? webViewController}) async {
  assert(url.toString().isNotEmpty);

  webViewController = webViewController ?? iosBelow11WebViewController;

  if (await _shouldUseJavascript()) {
    List<Cookie> cookies = await _getCookiesWithJavaScript(
        url: url, webViewController: webViewController);
    for (var i = 0; i < cookies.length; i++) {
      await _setCookieWithJavaScript(
          url: url,
          name: cookies[i].name,
          value: "",
          path: path,
          domain: domain,
          maxAge: -1,
          webViewController: webViewController);
    }
    return;
  }

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