How to manipulate / clear cookies with Capacitor Browser plugin

2.5k views Asked by At

I have a need to clear cookies for a website I opened with Capacitor Browser Plugin. I cannot find the way to do it.

In Capacitor Browser plugin there's a "browserPageLoaded" listener which is an ideal place to put my code in, but still i can't find any way to access cookies. How can you access cookies from there ?

1

There are 1 answers

1
Michele Ordile On BEST ANSWER

Unfortunately it is not possible to do this, the browser plugin uses SFSafariViewController on iOS and Chrome Custom Tabs on Android which for security reasons do not share cookies, see answer here https://github.com/ionic-team/capacitor-plugins/issues/7. To solve this problem you could use cordova's inAppBrowser which also works with Capacitor https://ionicframework.com/docs/native/in-app-browser. Among the options there is "clearcache" and "clearsessioncache". The list of options and examples are available at the following link https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/.

Example:

Console:

npm install cordova-plugin-inappbrowser
npm install @ionic-native/in-app-browser
ionic cap sync

Code:

import {
  InAppBrowser,
  InAppBrowserObject,
  InAppBrowserOptions,
} from "@ionic-native/in-app-browser/ngx";

constructor(private iab: InAppBrowser) {}

createNewPageBrowserInApp(url, target) {
    if (this.iab) {
      this.currentPageInAppBrowser = this.iab.create(
        url,
        target,
        this.options(inAppBrowserType)
      );
    }
  }

options(inAppBrowserType: InAppBrowserType): InAppBrowserOptions {
    switch (inAppBrowserType) {
      case InAppBrowserType.NavigationPage: {
        console.log("InAppBrowserType.NavigationPage");
        return {
          toolbarcolor: "#FFFFFF",
          location: "yes",
          hidden: "no",
          clearcache: "yes",
          clearsessioncache: "yes",
          zoom: "yes", //Android only
          hardwareback: "yes",
          mediaPlaybackRequiresUserAction: "no",
          hidenavigationbuttons: "no",
          navigationbuttoncolor: "#428bca",
          hideurlbar: "yes",
          shouldPauseOnSuspend: "no", //Android only
          disallowoverscroll: "no", //iOS only
          usewkwebview: "yes", //iOS only
          toolbar: "yes", //iOS only
          enableViewportScale: "no", //iOS only
          allowInlineMediaPlayback: "yes", //iOS only
          presentationstyle: "pagesheet", //iOS only
          fullscreen: "no", //Windows only
          useWideViewPort: "yes",
        };
      }

InAppBrowserType is an enum type for internal use