pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.25k stars 1.6k forks source link

Cookies not persisted if app is closed after cookies are set or deleted #2355

Open EArminjon opened 16 hours ago

EArminjon commented 16 hours ago

Is there an existing issue for this?

Current Behavior

All cookies are not available on app start.

Expected Behavior

Cookies should be accessible on app start. Maybe cookies are not fully loaded in memory when the call CookieManager.instance().getCookie is made.

Steps with code example to reproduce

  1. Launch the app
  2. You will see 'Cookie consent page'
  3. Check on log Cookie: null
  4. Click on 'accept cookie consent'
  5. 'Cookie consent page' is closed
  6. Check on log Cookie page closed, checking again cookie... and Cookie accepted !
  7. Cookie is well detected on flutter side
  8. Close the app
  9. Launch the app
  10. You see again 'Cookie consent page'
  11. Check on log Cookie: null
  12. Click on appbar back button
  13. Check on log Cookie page closed, checking again cookie... and Cookie accepted !
Code example to reproduce ```dart import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; // // // // // // // // // // // // const String url = 'https://coral-caresa-55.tiiny.site/'; void main() => runApp( const MaterialApp( home: Home(), ), ); class Home extends StatefulWidget { const Home({super.key}); @override State createState() => _HomeState(); } class _HomeState extends State { bool? cookieAccepted; @override void initState() { super.initState(); checkCookieConsent(); } Future checkCookieConsent({bool canOpenWebview = true}) async { final Cookie? cookie = await CookieManager.instance().getCookie( url: WebUri(url), name: 'consent', ); print('Cookie: $cookie'); if (cookie == null && canOpenWebview) { await Navigator.push( context, MaterialPageRoute( builder: (BuildContext context) => const CookieConsentPage(), ), ); print('Cookie page closed, checking again cookie...'); return checkCookieConsent(canOpenWebview: false); } else if (cookie == null) { print("Cookie not accepted !"); setState(() => cookieAccepted = false); } else { print("Cookie accepted !"); setState(() => cookieAccepted = true); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('home'), ), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ cookieAccepted == null ? const CircularProgressIndicator() : Text('Cookie accepted : $cookieAccepted'), const SizedBox(height: 16), ElevatedButton( onPressed: () async { setState(() => cookieAccepted = null); checkCookieConsent(canOpenWebview: false); }, child: const Text('Refresh cookie'), ), const SizedBox(height: 16), ElevatedButton( onPressed: () async { setState(() => cookieAccepted = null); checkCookieConsent(); }, child: const Text('Retry'), ), const SizedBox(height: 16), ElevatedButton( onPressed: () async { await CookieManager.instance().deleteCookie( url: WebUri(url), name: 'consent', ); setState(() => cookieAccepted = null); checkCookieConsent(); }, child: const Text('Delete cookie'), ), ], ), ), ); } } class CookieConsentPage extends StatelessWidget { const CookieConsentPage({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Cookie consent page'), ), body: InAppWebView( initialSettings: InAppWebViewSettings( javaScriptEnabled: true, ), onWebViewCreated: (InAppWebViewController controller) { controller.addJavaScriptHandler( handlerName: 'cookieAccepted', callback: (_) { Navigator.of(context).pop(true); }, ); controller.addUserScript( userScript: UserScript( source: """ window.addEventListener('message', function(event) { window.flutter_inappwebview.callHandler('cookieAccepted', true); }); """, forMainFrameOnly: true, injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START, ), ); }, initialUrlRequest: URLRequest( url: WebUri(url), ), ), ); } } ```

Demo :

You can see user accepting cookies, closing the app and then on launch cookie consent page is sadly again visible.

https://github.com/user-attachments/assets/c3de05e4-d5d2-4ea7-a263-41711864372b

Stacktrace/Logs

No applicable.

Flutter version

3.24.3

Operating System, Device-specific and/or Tool

iOS 18 simulator Xcode 16

Android 14 API 34

Plugin version

6.1.5

Additional information

[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0.1 24A348 darwin-arm64, locale fr-FR)
    • Flutter version 3.24.3 on channel stable at /Users/earminjon/fvm/versions/3.24.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (5 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/earminjon/Library/Android/Sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/earminjon/Library/Android/Sdk
    • Java binary at: /Users/earminjon/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16A242d
    • CocoaPods version 1.14.3

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

[✓] Android Studio (version 2024.1)
    • Android Studio at /Users/earminjon/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.11+0-17.0.11b1207.24-11852314)

[✓] Android Studio (version 2023.2)
    • Android Studio at /Users/perso/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.9+0-17.0.9b1087.7-11185874)

[✓] IntelliJ IDEA Ultimate Edition (version 2024.2.3)
    • IntelliJ at /Users/earminjon/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 82.0.3
    • Dart plugin version 242.22855.32

[✓] Connected device (5 available)
    • iPhone 16 Plus (mobile)         • A99393A6-DE51-45D5-8E1E-B077F59C1E57 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-18-0 (simulator)
    • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 15.0.1 24A348 darwin-arm64

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

• No issues found!

Self grab

EArminjon commented 15 hours ago

A potential workaround for android is present here : https://github.com/pichillilorenzo/flutter_inappwebview/issues/2354#issuecomment-2419966522.

For iOS i didn't know.

qkreltms commented 3 hours ago

I'm using flutter_inappwebview: 6.0.0 and have same issue in IOS and Android

qkreltms commented 2 hours ago

It's was my fault. there was 'CookieManager.instance().deleteCookies(url: ...));' in my onViewCreated