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

setcookie ios failed #118

Closed
Hiram-Y opened this issue Jun 18, 2019 · 16 comments
Closed

setcookie ios failed #118

Hiram-Y opened this issue Jun 18, 2019 · 16 comments

Comments

@Hiram-Y
Copy link

Hiram-Y commented Jun 18, 2019

CookieManager.setCookie(http://m.test.com, "token", "h7dcc4islpbfigvpg25aqq7ru4",domain: 'm.test.com',expiresDate: time);

 CookieManager.setCookie(http://m.test.com, "phone", "111222333",domain: 'm.test.com',expiresDate: time);

 CookieManager.setCookie(http://m.test.com, "code", "223453",domain: 'm.test.com',expiresDate: time);
@Hiram-Y
Copy link
Author

Hiram-Y commented Jun 18, 2019

by

 onLoadStop: (InAppWebViewController controller, String url) async {
             print(await controller.injectScriptCode("document.cookie"));
          },

print Hm_lpvt_14d35254d0c1854c4be8cb1155be9fe8=1560851694; Hm_lvt_14d35254d0c1854c4be8cb1155be9fe8=1560850617,1560850626,1560851685,1560851694; token=mkunnarvgtbmf85vj304978v17
my setcookie ios is failed , but android is ok。

@daryll-fourie
Copy link

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

@oliverbytes
Copy link

setCookie still doesn't seem to work for iOS. It successfully saves it to the CookieManager but doesn't actually work on the WebView

@pichillilorenzo
Copy link
Owner

@nemoryoliver @daryll-fourie @Hiram-Y 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.

A full example 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

@oliverbytes
Copy link

oliverbytes commented Dec 8, 2019

@nemoryoliver @daryll-fourie @Hiram-Y 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.

A full example 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

It also successfully sets and gets it from CookieManager. but it weirdly doesn't get passed in the actual webview's cookie. I think I tried printing the actual document.cookie using evaluate javascript. I've also moved to that package already

@pichillilorenzo
Copy link
Owner

@nemoryoliver Can you share the code you tested and didn't work? Also, the device information and Xcode version, please? Thanks!

@wj1234
Copy link

wj1234 commented Dec 12, 2019

url must be https

This was referenced Jul 6, 2020
@Kimhak-Loem
Copy link

Kimhak-Loem commented Nov 9, 2020

@nemoryoliver excuse me, did you able to let CookieManager work with the webview with iOS. I'm currently having the same issue with iOS. It successfully saves it to the CookieManager but not able to see in controller.evaluateJavascript(source: "document.cookie")

Any help would be appreciated,

@oliverbytes
Copy link

@nemoryoliver excuse me, did you able to let CookieManager work with the webview with iOS. I'm currently having the same issue with iOS. It successfully saves it to the CookieManager but not able to see in controller.evaluateJavascript(source: "document.cookie")

Any help would be appreciated,

sorry for the late response. what I did was used the https://pub.dev/packages/webview_flutter package plus the https://pub.dev/packages/webview_cookie_manager they work brilliant with cookies

@lugael
Copy link

lugael commented Oct 13, 2022

Hi, any news on this issue?
I performed some tests, on Android everything is fine, but on IOS when it's for an HTTPS URL don't set the cookie.

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Oct 13, 2022

@lugael please, provide the example code to test for iOS!

This is my simple test code on iOS 16.0 and it works as expected:

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

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final CookieManager cookieManager = CookieManager.instance();

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

  runApp(MaterialApp(
      home: new MyApp()
  ));
}

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

class _MyAppState extends State<MyApp> {
  final GlobalKey webViewKey = GlobalKey();

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("CookieManager example")),
        body: SafeArea(
            child: Column(children: <Widget>[
              Expanded(
                child: InAppWebView(
                  key: webViewKey,
                  initialUrlRequest: URLRequest(url: Uri.parse("https://flutter.dev/")),
                  onLoadStop: (controller, url) async {
                    print(await controller.evaluateJavascript(source: "document.cookie"));
                  },
                  onConsoleMessage: (controller, consoleMessage) {
                    print(consoleMessage);
                  },
                ),
              ),
            ]))
    );
  }
}

It prints correctly my custom cookies:

flutter: _ga=GA1.2.712922374.1665577422; _gat=1; _gid=GA1.2.1531628081.1665577422; myCookie=myValue; myToken=wertygubnm5046rhndf; myCookie2=myValue2

@lugael
Copy link

lugael commented Oct 13, 2022

@pichillilorenzo Here's an example of IOS, test on an iPhone 13 IOS 16

String url = https://homologacao-morador.apcontrole.com.br
String domain = apcontrole.com.br
String name = _onesignal-player-identifier
String value = UUidd

await _cookieManager .setCookie(
url: Uri.parse(url),
domain: domain,
name: name,
value: value,
isSecure: true,
isHttpOnly: true,
expiresDate: expires is DateTime ? expires.millisecondsSinceEpoch : DateTime.now().add(Duration(days: 1)).millisecondsSinceEpoch,
path: "/")
.catchError((error) {
print(error);
});
On android I have 8 cookies and 5 of them I put through the app:
Android

But on IOS only the website is left, the app doesn't set any cookies and doesn't show an error:
IOS

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Oct 13, 2022

@lugael you need to set the domain as .apcontrole.com.br, with a dot in front!
If the dot is omitted, it will not include subdomains.

@lugael
Copy link

lugael commented Oct 13, 2022

It's work for me thanks

@fabienbranchel
Copy link

I don't have to handle any subdomain in my case but anyway, specify domain seems to be the key to make it works for iOS.

_cookieManager.setCookie(
    url: InAppWebview.WebUri(cookie.domain),
    domain: cookie.domain,
    name: cookie.name,
    value: cookie.value,
);

Copy link

github-actions bot commented Oct 3, 2024

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 Oct 3, 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

8 participants