fadhli-sulaimi
fadhli-sulaimi

Reputation: 716

Unable to get cookies for flutter iOS webview

I am fetching cookies from a login page of a website using flutter webview inside my stateful widget. So after the user logins, and the url changes the cookies will be fetched. Right now I am unable to fetch cookies when my app is in iOS. By default in android, chrome webview is launched, while in iOS the wkwebview or safari webview is launched i think.

I am using the flutter inappbrowser plugin which has cookie manager built in. I tried changing the user agent but still same issue.

Plugin:

https://pub.dartlang.org/packages/flutter_inappbrowser

I am using the same codes and i can fetch cookies in Android but not in iOS

import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';

MyInAppBrowser inAppBrowser = new MyInAppBrowser();  

class MainScreen extends State<MainScreen> {

  @override
  void initState() {
    _openWebView();
  }

  _openWebView() async{
     await inAppBrowser.open(url: "https://*****some 
      website*****/logon.php", options: {
      "javaScriptEnabled":true,
      "clearCache":true,
      "useShouldOverrideUrlLoading": true,
      "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
       AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77",
    });
  }
}

class MyInAppBrowser extends InAppBrowser {

   @override
   void onBrowserCreated() async {
      print("\n\nBrowser Ready!\n\n");
   }

   @override
   void onLoadStart(String url) async {
      CookieManager.getCookies(url).then((val) {
        debugPrint("COOKIEMANAGER: " + val.toString());
     });
   }

   @override
   void shouldOverrideUrlLoading(String url) {
      CookieManager.getCookies(url).then((val) {
        debugPrint("COOKIEMANAGER: " + val.toString());
      });

   }

   @override
   void onLoadError(String url, int code, String message) {
      print("\n\nCan't load $url.. Error: $message\n\n");
    }

    @override
    void onExit() {
        print("\n\nBrowser closed!\n\n");
    }

}

I should be able to get cookies info such as userid, sessionhash etc which is shown on Android

Upvotes: 2

Views: 6449

Answers (1)

Omatt
Omatt

Reputation: 10519

The plugin that you're using (flutter_inappbrowser) seems to be outdated and has been discontinued. I suggest using webview_flutter as it has support for this feature and has just been recently out of developer preview.

To fetch cookies using webview_flutter, you can call WebViewController.evaluateJavascript('document.cookie');. The method evaluateJavascript is used to evaluate the JavaScript expression 'document.cookie' of the page and returns a String value. You can check the sample in this page to try it out.

Upvotes: 0

Related Questions