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

Adding a cookie not working on ios? #338

Closed
SwannLV opened this issue May 11, 2020 · 7 comments
Closed

Adding a cookie not working on ios? #338

SwannLV opened this issue May 11, 2020 · 7 comments

Comments

@SwannLV
Copy link

SwannLV commented May 11, 2020

Environment

Flutter version: 1.17
Plugin version: 3.1.0
iOS version: 13.3
Device: Iphone11 simulator

Description

Adding a cookie is working for me on android but not on ios simulator.

Here is my code:

  final CookieManager _cookieManager = CookieManager.instance();

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

    final expiresDate =
        DateTime.now().add(Duration(days: 3)).millisecondsSinceEpoch;

    _cookieManager.deleteAllCookies();

    _cookieManager.setCookie(
      url: env.webAppUrl,
      name: "session",
      value: ClientProvider.token,
      domain: env.webAppCookieDomain,
      expiresDate: expiresDate,
      isSecure: true,
    );
  }

Have I missed some configuration?

Thanks a lot!

@pichillilorenzo
Copy link
Owner

That's because you are deleteAllCookies and setCookie are async methods. You don't know which will be called first by the platform.
So, you need first call deleteAllCookies and then setCookie using await operator or then method on the deleteAllCookies result.

However, I noticed that expiresDate option is not working correctly on both iOS and Android platforms. I already fixed it and it will be available on the next release.

Here is an example using then method:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  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(
        initialRoute: '/',
        routes: {
          '/': (context) => CookieManagerExample()
        }
    );
  }
}

class CookieManagerExample extends StatefulWidget {
  final CookieManagerExampleState state = CookieManagerExampleState();

  @override
  CookieManagerExampleState createState() => state;
}

class CookieManagerExampleState extends State<CookieManagerExample> {
  InAppWebViewController webViewController;
  final CookieManager _cookieManager = CookieManager.instance();

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

    final expiresDate =
        DateTime.now().add(Duration(days: 3)).millisecondsSinceEpoch;

    _cookieManager.deleteAllCookies().then((value) {
      _cookieManager.setCookie(
        url: "https://flutter.dev/",
        name: "session",
        value: "54th5hfdcfg34",
        domain: ".flutter.dev",
        expiresDate: expiresDate,
        isSecure: true,
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                    initialUrl: "https://flutter.dev/",
                    initialHeaders: {},
                    initialOptions: InAppWebViewGroupOptions(
                        crossPlatform: InAppWebViewOptions(
                            debuggingEnabled: true
                        )
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webViewController = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) async {
                      List<Cookie> cookies = await _cookieManager.getCookies(url: url);
                      cookies.forEach((cookie) {
                        print(cookie.name + " " + cookie.value);
                      });
                    },
                  ),
                ),
              ),
            ])
        )
    );
  }
}

pichillilorenzo pushed a commit that referenced this issue May 11, 2020
expiresDate option (#338)
@jagged91
Copy link

@pichillilorenzo Any idea when this release will be up? I'm unable to set a cookie either in Android or iOS. I'm using await in an async function to first delete all cookies and then set them. When I run 'getCookie' immediately after, the cookie doesn't exist...

@jagged91
Copy link

jagged91 commented May 11, 2020

    await _cookieManager.deleteAllCookies();
    await _cookieManager.setCookie(
        url:
            "url",
        name: "token",
        value: "value,
        domain: "domain",
        isSecure: true,
        expiresDate: anHourFromNow
        );

    var cookieData = await _cookieManager.getCookie(url:"url", name: "token");

Here's an example of what I'm doing, with some data redacted. When I print the list of cookies like you did in your example above, I don't see the cookie I just set. In addition, 'cookieData' is null.

@SwannLV
Copy link
Author

SwannLV commented May 11, 2020

@pichillilorenzo thanks a lot for your answer.
It does not seem to work on ios for me, chaining with a .then(..
Maybe it is because I am on the simulator?
Will try on a real device...

@pichillilorenzo
Copy link
Owner

@SwannLV I tested it on iOS simulator and it is working.

@SwannLV and @jgadsby could you try in a fresh app the example code I gave to you?
If the cookie for "https://flutter.dev/" url is set, then CookieManager is working. If it works with my example but not in your use case, then there is something else. In that case, I need more info about your use case (full code example that for you doesn't work), otherwise, I don't know what's going on and why it is not working for you.

@SwannLV
Copy link
Author

SwannLV commented May 11, 2020

I found my problem: cookie.domain not starting with a dot does seem to work on ios.
Thank you very much @pichillilorenzo for everything 🙏

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants