Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CookieManager #50

Open
MattHamburger opened this issue Mar 5, 2019 · 7 comments
Open

CookieManager #50

MattHamburger opened this issue Mar 5, 2019 · 7 comments

Comments

@MattHamburger
Copy link

In my app I am logging in to a server using HttpClient in http.dart. I get two cookies from the server and use them in all requests.
I need to use embedded web views in several places so I am trying to use the inappbrowser.

Is there a way to set the cookies?

@pichillilorenzo
Copy link
Owner

Did you try the CookieManager class already?

@MattHamburger
Copy link
Author

I did but I am not sure how to use it, I have a loop where I get the cookie key/values and within the loop I have:
CookieManager.setCookie(
"https://ev01-data1.mycompany.com:446", cookie.name, cookie.value,
domain: ".mycompany.com",
isSecure: true,
path: "/",
)
This is called two times with a different key/value.

@daryll-fourie
Copy link

Any update on this? I'm also getting this issue...

@VincentTung1
Copy link

VincentTung1 commented Dec 6, 2019

Hello,I have ever occurred this problem for a long time , util now, I could not find any solution.
I have ever tried to put the cookie name and value to the header, just could verify in home page.
But,if its child page needs to take the cookie for sub verify, it failed.
I take this question is to find the solution for IOS Device,it seems to cannot read the cookies successfully.I really want to find some solutions from yours.Thanks!

@pichillilorenzo
Copy link
Owner

@MattHamburger Try the latest version of the plugin. Also, this plugin changed its name to flutter_inappwebview. The current latest version now is 2.1.0+1. So, you can change your dependency influtter_inappwebview: ^2.1.0+1.

@VincentTung1
A full example where I set custom cookies that I tried on both Android (API level 28) and iOS (version 13.1) platforms and it works for both:

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: InAppWebViewPage()
    );
  }
}

class InAppWebViewPage extends StatefulWidget {
  @override
  _InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
  InAppWebViewController webView;
  final CookieManager cookieManager = CookieManager.instance();

  @override
  void initState() {
    super.initState();

    var expiresDate = DateTime.parse("2019-09-30 00:00:00.000").millisecondsSinceEpoch;
    cookieManager.setCookie(url: "https://flutter.dev", name: "myToken", value: "wertygubnm5046rhndf", domain: ".flutter.dev", expiresDate: expiresDate);
    cookieManager.setCookie(url: "https://flutter.dev", name: "myCookie", value: "myValue", domain: ".flutter.dev", expiresDate: expiresDate);
    cookieManager.setCookie(url: "https://flutter.dev", name: "myCookie2", value: "myValue2");
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: SafeArea(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://flutter.dev",
                    initialHeaders: {},
                    initialOptions: InAppWebViewWidgetOptions(
                      inAppWebViewOptions: InAppWebViewOptions(
                        debuggingEnabled: true,
                      )
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) async {
                      print(await controller.evaluateJavascript(source: "document.cookie"));
                    },
                  ),
                ),
              ),
              ButtonBar(
                alignment: MainAxisAlignment.center,
                children: <Widget>[
                  RaisedButton(
                    child: Icon(Icons.arrow_back),
                    onPressed: () {
                      if (webView != null) {
                        webView.goBack();
                      }
                    },
                  ),
                  RaisedButton(
                    child: Icon(Icons.arrow_forward),
                    onPressed: () {
                      if (webView != null) {
                        webView.goForward();
                      }
                    },
                  ),
                  RaisedButton(
                    child: Icon(Icons.refresh),
                    onPressed: () {
                      if (webView != null) {
                        webView.reload();
                      }
                    },
                  ),
                ],
              ),
            ]))
    );
  }
}

It prints all of my cookies:

myCookie=myValue; myToken=wertygubnm5046rhndf; myCookie2=myValue2; _ga=GA1.2.919201585.1575042727; _gid=GA1.2.2019611942.1575737332; _gat=1

@VincentTung1
Copy link

@MattHamburger Try the latest version of the plugin. Also, this plugin changed its name to flutter_inappwebview. The current latest version now is 2.1.0+1. So, you can change your dependency influtter_inappwebview: ^2.1.0+1.

@VincentTung1
A full example where I set custom cookies that I tried on both Android (API level 28) and iOS (version 13.1) platforms and it works for both:

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: InAppWebViewPage()
    );
  }
}

class InAppWebViewPage extends StatefulWidget {
  @override
  _InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
  InAppWebViewController webView;
  final CookieManager cookieManager = CookieManager.instance();

  @override
  void initState() {
    super.initState();

    var expiresDate = DateTime.parse("2019-09-30 00:00:00.000").millisecondsSinceEpoch;
    cookieManager.setCookie(url: "https://flutter.dev", name: "myToken", value: "wertygubnm5046rhndf", domain: ".flutter.dev", expiresDate: expiresDate);
    cookieManager.setCookie(url: "https://flutter.dev", name: "myCookie", value: "myValue", domain: ".flutter.dev", expiresDate: expiresDate);
    cookieManager.setCookie(url: "https://flutter.dev", name: "myCookie2", value: "myValue2");
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: SafeArea(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://flutter.dev",
                    initialHeaders: {},
                    initialOptions: InAppWebViewWidgetOptions(
                      inAppWebViewOptions: InAppWebViewOptions(
                        debuggingEnabled: true,
                      )
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) async {
                      print(await controller.evaluateJavascript(source: "document.cookie"));
                    },
                  ),
                ),
              ),
              ButtonBar(
                alignment: MainAxisAlignment.center,
                children: <Widget>[
                  RaisedButton(
                    child: Icon(Icons.arrow_back),
                    onPressed: () {
                      if (webView != null) {
                        webView.goBack();
                      }
                    },
                  ),
                  RaisedButton(
                    child: Icon(Icons.arrow_forward),
                    onPressed: () {
                      if (webView != null) {
                        webView.goForward();
                      }
                    },
                  ),
                  RaisedButton(
                    child: Icon(Icons.refresh),
                    onPressed: () {
                      if (webView != null) {
                        webView.reload();
                      }
                    },
                  ),
                ],
              ),
            ]))
    );
  }
}

It prints all of my cookies:

myCookie=myValue; myToken=wertygubnm5046rhndf; myCookie2=myValue2; _ga=GA1.2.919201585.1575042727; _gid=GA1.2.2019611942.1575737332; _gat=1

But I have tried your code for testing again, unfortunately,it still no work.I simply want to set the cookie named "JSESSIONID"。Have you ever tried this key ? I really want to solve the problem, or can you give me your any contact way for me ?

@VincentTung1
Copy link

Does the cookieManager must set in the method "initState"?

This was referenced Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants