flutter_inappwebview icon indicating copy to clipboard operation
flutter_inappwebview copied to clipboard

shouldOverrideUrlLoading not working on Android

Open fyxtc opened this issue 1 year ago • 2 comments

void main() {
  runApp(const MaterialApp(home: Scaffold(body: WebViewDemo())));
}

class WebViewDemo extends StatefulWidget {
  const WebViewDemo({super.key});
  @override
  State<WebViewDemo> createState() => _WebViewDemoState();
}

class _WebViewDemoState extends State<WebViewDemo> {
  HeadlessInAppWebView? headlessWebView;

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

    headlessWebView = HeadlessInAppWebView(
      initialUrlRequest: URLRequest(url: WebUri("https://inappwebview.dev/")),
      initialSettings: InAppWebViewSettings(isInspectable: kDebugMode),
      onReceivedError: (controller, request, error) {
        print("onReceivedError $request >>>>>>> $error");
      },
      shouldOverrideUrlLoading: (controller, navigationAction) async {
        final navUrl = navigationAction.request.url.toString();
        print("shouldOverrideUrlLoading >>>>>>> $navUrl");
      },
      onLoadStart: (controller, url) {
        print("onLoadStart >>>>>>> $url");
      },
      onLoadStop: (controller, url) {
        print("onLoadStop >>>>>>> $url");
      },
    );

    headlessWebView?.run();
    Timer(Duration(seconds: 3), () {
      headlessWebView!.webViewController!
          .loadUrl(urlRequest: URLRequest(url: WebUri("https://inappwebview.dev/")));
    });
  }

  @override
  Widget build(BuildContext context) {
    return const Center(child: Text("WebView Demo"));
  }

  @override
  void dispose() {
    super.dispose();
    headlessWebView?.dispose();
  }
}
截屏2024-01-09 17 28 01

fyxtc avatar Jan 09 '24 09:01 fyxtc

👋 @fyxtc

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

github-actions[bot] avatar Jan 09 '24 09:01 github-actions[bot]

Normal InAppWebView Case:

class WebViewDemo extends StatefulWidget {
  const WebViewDemo({super.key});
  @override
  State<WebViewDemo> createState() => _WebViewDemoState();
}

class _WebViewDemoState extends State<WebViewDemo> {
  InAppWebView? headlessWebView;
  InAppWebViewController? controller;
  @override
  void initState() {
    super.initState();

    headlessWebView = InAppWebView(
      onWebViewCreated: (controller) {
        this.controller = controller;
      },
      initialUrlRequest: URLRequest(url: WebUri("https://inappwebview.dev/")),
      initialSettings: InAppWebViewSettings(isInspectable: kDebugMode),
      onReceivedError: (controller, request, error) {
        print("onReceivedError $request >>>>>>> $error");
      },
      shouldOverrideUrlLoading: (controller, navigationAction) async {
        final navUrl = navigationAction.request.url.toString();
        print("shouldOverrideUrlLoading >>>>>>> $navUrl");
      },
      onLoadStart: (controller, url) {
        print("onLoadStart >>>>>>> $url");
      },
      onLoadStop: (controller, url) {
        print("onLoadStop >>>>>>> $url");
        if (url.toString() == "https://inappwebview.dev/") {
          controller.loadUrl(urlRequest: URLRequest(url: WebUri("https://cn.bing.com")));
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // return const Center(child: Text("WebView Demo"));
    return Container(
      child: headlessWebView,
    );
  }

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

截屏2024-01-09 17 42 26

Android 9 API 28 Flutter: 3.13.7 InAppWebView: 6.0.0

fyxtc avatar Jan 09 '24 09:01 fyxtc

@fyxtc I think you should set initialOptions like this.

InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
        mediaPlaybackRequiresUserGesture: false,
      ),
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ),
      ios: IOSInAppWebViewOptions(
        allowsInlineMediaPlayback: true,
      ));

headlessWebView = InAppWebView(
    initialOptions: options,
    ....
)

However, in my 5.x.x series environment, useShouldOverrideUrlLoading works on iOS, but not on Android. Does anyone else have the same symptoms?

RyotaroIsoyama avatar Feb 26 '24 08:02 RyotaroIsoyama

However, in my 5.x.x series environment, useShouldOverrideUrlLoading works on iOS, but not on Android. Does anyone else have the same symptoms?

That was caused by my local network problems. sorry.

RyotaroIsoyama avatar Feb 27 '24 06:02 RyotaroIsoyama