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

ChromeSafariBrowser isn't calling its events, and not keeping track of isOpen properly #759

Closed
4 tasks done
hworld opened this issue Mar 26, 2021 · 8 comments
Closed
4 tasks done
Labels
bug Something isn't working

Comments

@hworld
Copy link

hworld commented Mar 26, 2021

Environment

Technology Version
Flutter version 2.0.2
Plugin version 5.2.1+1
Android version 11
iOS version n/a
Xcode version n/a

Device information: Pixel 5

Description

onOpened onCompletedInitialLoad and onClosed are not being called for me. It seems to be because handleMethod is never being called. Because of that, it's also not keeping track of _isOpened properly. It's only letting me open the browser once since it thinks it's still opened even after closing.

Steps to reproduce

I basically took the example.

final webBrowser = AppWebBrowser();

// The below events never get called for me.
class AppWebBrowser extends ChromeSafariBrowser {
  @override
  void onOpened() {
    print("ChromeSafari browser opened");
  }

  @override
  void onCompletedInitialLoad() {
    print("ChromeSafari browser initial load completed");
  }

  @override
  void onClosed() {
    print("ChromeSafari browser closed");
  }
}

// Then somewhere, you can call this. If you close the browser and then trigger the open again a second time, it'll fail.
// webBrowser.open(url: Uri.parse('https://google.com'));

Stacktrace/Logcat

E/flutter (25930): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: ChromeSafariBrowserAlreadyOpenedException: [Error: Cannot open https://google.com! The browser is already opened.]
E/flutter (25930): #0      ChromeSafariBrowser.throwIsAlreadyOpened
package:flutter_inappwebview/…/chrome_safari_browser/chrome_safari_browser.dart:148
E/flutter (25930): #1      ChromeSafariBrowser.open
package:flutter_inappwebview/…/chrome_safari_browser/chrome_safari_browser.dart:90
@hworld hworld added the bug Something isn't working label Mar 26, 2021
@pichillilorenzo
Copy link
Owner

As you can see from the error, you can't open 2 times the same instance at the same moment if it's already opened. That's right and it's the expected behavior.
If you want to open it 2 times, you need to create another instance of ChromeSafariBrowser.
For example:

final webBrowser1 = AppWebBrowser();
final webBrowser2 = AppWebBrowser();

// ... other code ...

webBrowser1.open(url: Uri.parse('https://google.com'));

webBrowser2.open(url: Uri.parse('https://flutter.dev'));

@hworld
Copy link
Author

hworld commented Mar 26, 2021

Sorry, I think you misunderstood. I actually close the web browser before calling open again, by pressing the X button on the tab. The real issue seems to be that the events are not being called at all.

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Mar 26, 2021

I'm unable to reproduce your problem.

This is my code example tested on Android Emulator API 30:

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

class AppWebBrowser extends ChromeSafariBrowser {
  @override
  void onOpened() {
    print("ChromeSafari browser opened");
  }

  @override
  void onCompletedInitialLoad() {
    print("ChromeSafari browser initial load completed");
  }

  @override
  void onClosed() {
    print("ChromeSafari browser closed");
  }
}

final webBrowser = AppWebBrowser();

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MaterialApp(home: new MyApp()));
}

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

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('ChromeSafariBrowser Example'),
        ),
        body: Center(
          child: ElevatedButton(
            child: Text("Open Browser"),
            onPressed: () async {
              await webBrowser.open(url: Uri.parse('https://google.com'));
            },
          ),
        ));
  }
}

This is the screen recording:

Registrazione.schermo.2021-03-26.alle.20.54.29.mov

@hworld
Copy link
Author

hworld commented Mar 26, 2021

Hmm, thanks for the quick answer. It must be something else messing with it in my app then. I'll report back if I get more data on when it's failing.

@no-response no-response bot removed the answered label Mar 26, 2021
@pichillilorenzo
Copy link
Owner

If possible, share your code or create the exact steps to reproduce that error using an example code.

@hworld
Copy link
Author

hworld commented Mar 26, 2021

Okay, I figured out the exact spot where it no longer works in my codebase. I was able to take your example and break it. Something is actually happening somewhere when attaching FirebaseMessaging with a background message handler.

class AppWebBrowser extends ChromeSafariBrowser {
  @override
  void onOpened() {
    print("ChromeSafari browser opened");
  }

  @override
  void onCompletedInitialLoad() {
    print("ChromeSafari browser initial load completed");
  }

  @override
  void onClosed() {
    print("ChromeSafari browser closed");
  }
}

final webBrowser = AppWebBrowser();

Future<void> _backgroundHandler(RemoteMessage message) async {
  print('Got background message');
}

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(_backgroundHandler);
  runApp(MaterialApp(home: new MyApp()));
}

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

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('ChromeSafariBrowser Example'),
        ),
        body: Center(
          child: ElevatedButton(
            child: Text("Open Browser"),
            onPressed: () async {
              await webBrowser.open(url: Uri.parse('https://google.com'));
            },
          ),
        ));
  }
}

My pubspec has these dependencies:

firebase_core: 1.0.1
firebase_messaging: 9.0.0
flutter_inappwebview: 5.2.1+1

I'm not too knowledgeable about the native side to know why this might be conflicting. You can see I'm not even doing anything in the background message handler for Firebase.

@pichillilorenzo
Copy link
Owner

Fixed with new release 5.3.0!

@hworld
Copy link
Author

hworld commented Mar 28, 2021

Thanks so much! The fix works great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants