flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.32k stars 27.28k forks source link

[webview_flutter] Flutter 3 iOS WebView initialCookies are not being set #115460

Open rsp-84 opened 1 year ago

rsp-84 commented 1 year ago

When setting initialCookies on a WebView with Flutter 3 on an iOS device the cookie(s) are not being applied. It seems to only affect iOS. Android is still working as expected.

Steps to Reproduce

  1. Run the Code Sample below with an iOS device
  2. Click on the button to open a WebView that is setting a test cookie
  3. Observe that the cookie is not being applied on an iOS device

Expected results: The test_cookie should be set on both iOS and Android. Currently it is only being set on Android with the initialCookies: param on WebView.

Actual results: The test_cookie on iOS is not being set.

Code sample ```dart import 'dart:io'; import 'package:flutter/material.dart'; import 'package:webview_flutter/webview_flutter.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ElevatedButton(onPressed: () => Navigator.push( context, MaterialPageRoute( builder: (context) => WebViewTestPage(), ), ), child: const Text('Push to open webview'))], ), ), ); } } class WebViewTestPage extends StatelessWidget { late WebViewController _controller; @override Widget build(BuildContext context) { return WebView( onPageFinished: (_) async { final cookie = await _controller.runJavascriptReturningResult('document.cookie'); _printCookie('Cookie: $cookie'); }, onWebViewCreated: ((controller) { _controller = controller; }), initialUrl: 'https://www.google.com', javascriptMode: JavascriptMode.unrestricted, debuggingEnabled: true, initialCookies: [WebViewCookie(domain: 'www.google.com', name: 'test_cookie', value: 'fjklafjdl')], ); } void _printCookie(String response) { print(response); } } ```
Console Logs ``` iOS: flutter: Cookie: 1P_JAR=2022-11-16-16; OGPC=19029186-12: Droid: I/flutter (21392): Cookie: "test_cookie=fjklafjdl; 1P_JAR=2022-11-16-16" ```
Flutter Doctor ``` [✓] Flutter (Channel unknown, 3.3.8, on macOS 11.7 20G817 darwin-x64, locale en-US) • Flutter version 3.3.8 on channel unknown at /Users/user/Development/flutter • Framework revision 52b3dc25f6 (8 days ago), 2022-11-09 12:09:26 +0800 • Engine revision 857bd6b74c • Dart version 2.18.4 • DevTools version 2.15.0 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/user/Library/Android/sdk • Platform android-33, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 13C100 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) [✓] VS Code (version 1.71.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.52.0 [✓] Connected device (5 available) • Android SDK built for x86 (mobile) • emulator-5554 • android-x86 • Android 10 (API 29) (emulator) • iPhone 12 (mobile) • 92663F14-DD56-4005-8577-54DD81C5F9AD • ios • com.apple.CoreSimulator.SimRuntime.iOS-15-2 (simulator) [✓] HTTP Host Availability • All required HTTP hosts are available ```
huycozy commented 1 year ago

@rsp-84 Thanks for filing the issue. This issue is reproducible on the latest stable and master channels with provided sample code.

✅: No Issue ❌: Issue reproduced

flutter doctor -v (stable and master) ```bash [✓] Flutter (Channel stable, 3.3.8, on macOS 13.0 22A380 darwin-x64, locale en-VN) • Flutter version 3.3.8 on channel stable at /Users/huynq/Documents/GitHub/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 52b3dc25f6 (23 hours ago), 2022-11-09 12:09:26 +0800 • Engine revision 857bd6b74c • Dart version 2.18.4 • DevTools version 2.15.0 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-33, build-tools 31.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14A400 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [✓] IntelliJ IDEA Community Edition (version 2022.2.2) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 70.0.5 • Dart plugin version 222.4167.21 [✓] IntelliJ IDEA Community Edition (version 2022.1.1) • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.73.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.52.0 [✓] Connected device (3 available) • iPhone 14 Pro Max (mobile) • 332079AF-E895-4685-910E-7B1E18B0C6B8 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-0 (simulator) • macOS (desktop) • macos • darwin-x64 • macOS 13.0 22A380 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ``` ```bash [!] Flutter (Channel master, 3.6.0-4.0.pre.26, on macOS 13.0 22A380 darwin-x64, locale en-VN) • Flutter version 3.6.0-4.0.pre.26 on channel master at /Users/huynq/Documents/GitHub/flutter_master ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision fafa11d45d (44 minutes ago), 2022-11-16 21:54:49 -0500 • Engine revision cb403c1fea • Dart version 2.19.0 (build 2.19.0-406.0.dev) • DevTools version 2.19.0 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-33, build-tools 31.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14A400 • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2021.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [✓] IntelliJ IDEA Community Edition (version 2022.2.2) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 70.0.5 • Dart plugin version 222.4167.21 [✓] IntelliJ IDEA Community Edition (version 2022.1.1) • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.73.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.52.0 [✓] Connected device (3 available) • SM T225 (mobile) • R9JT3004VRJ • android-arm64 • Android 12 (API 31) • macOS (desktop) • macos • darwin-x64 • macOS 13.0 22A380 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. ```
MarcinHradowicz commented 1 year ago

I've just migrated to 4.0.1, and it seems to work fine now. 👌

CRuiz-ObraLink commented 1 year ago

In version 4.2.0 this issue seems to still be unsolved. I have tried setting cookies with the WebViewCookieManager and it works on Android but fails on iOS.

Guchen1 commented 1 year ago

Same issue, quite severe

malbolged commented 1 year ago

Same issue, quite severe

image They don't think so ...

basti394 commented 7 months ago

Is there any solution yet? I have a similar issue as described by @CRuiz-ObraLink. When calling WebViewCookieManager.setCookie() it works fine on android, but on ios the cookies are not set. I use version 4.7.0

Flutter doctor:


[✓] Flutter (Channel stable, 3.19.1, on macOS 14.2.1 23C71 darwin-arm64, locale en-DE)
    • Flutter version 3.19.1 on channel stable at /Users/user/Flutter/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision abb292a07e (5 days ago), 2024-02-20 14:35:05 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Users/user/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Users/user/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.3)
    • IntelliJ at /Users/user/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 77.0.1
    • Dart plugin version 233.13763.5

[✓] Connected device (3 available)            
    • iPhone von Entwicklungsteam (mobile) • 00008030-001C1D3C0140C02E • ios            • iOS 15.1 19B74
    • macOS (desktop)                      • macos                     • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
    • Chrome (web)                         • chrome                    • web-javascript • Google Chrome 122.0.6261.69

[✓] Network resources
    • All expected network resources are available.

• No issues fou
```nd!
bawahakim commented 6 months ago

I have the same issue. Cookies work on Android but not ios. Would really hope for a fix or at least a workaround :)

CRuiz-ObraLink commented 6 months ago

We ended up using a workaround by passing the cookies inside the headers of the WebViewController.loadRequest as well. That worked for iOS.

rnisthal commented 5 months ago

We ended up using a workaround by passing the cookies inside the headers of the WebViewController.loadRequest as well. That worked for iOS.

Could you please provide an example? For the life of me I cannot set a cookie on iOS

controller = WebViewController()
  ..setJavaScriptMode(JavaScriptMode.unrestricted)
  ..loadRequest(
      Uri.parse(
          'https://www.myserver.com/myurl'),
      headers: {
        'Cookie':
            'MyCookie=value; Domain=.myserver.com; SameSite=Lax'
      });
LiLatee commented 5 months ago

I am setting a cookies in such a way and it works fine on both Android and iOS:

final WebViewCookie cookie = WebViewCookie(
      name: cookieName,
      value: cookieValue,
      domain: cookieDomain,
    );
WebViewCookieManager.fromPlatformCreationParams(const PlatformWebViewCookieManagerCreationParams()).platform.setCookie(cookie);

Packages versions:

  webview_flutter: ^4.7.0
  webview_flutter_android: ^3.16.0
  webview_flutter_wkwebview: ^3.13.0