Finding a way to append query params to link opening in flutter InAppWebView

Viewed 1501

What I want is that whenever a link is opened in webview, it should be appended with client param. eg www.google.com?client=android or www.google.com?client=iphone Things I have tried: 1. cancel al request in shouldOverrideUrlLoading and request a new loadUrl on InAppWebViewController (App crashes on ios as soon as webview loads in this approach)

1 Answers

I have updated my plugin flutter_inappwebview! The latest release is 3.3.0+3 at the time of this writing.

You can achieve what you are asking through the shouldOverrideUrlLoading event (set the useShouldOverrideUrlLoading: true option) in this way:

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

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

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

class _MyAppState extends State<MyApp> {
  InAppWebViewController webView;

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                  child: InAppWebView(
                    initialUrl: "https://github.com/flutter?client=" + (Platform.isAndroid ? 'android': 'ios'),
                    initialHeaders: {},
                    initialOptions: InAppWebViewGroupOptions(
                      crossPlatform: InAppWebViewOptions(
                        debuggingEnabled: true,
                        useShouldOverrideUrlLoading: true
                      ),
                    ),
                    onWebViewCreated: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onLoadStart: (InAppWebViewController controller, String url) {

                    },
                    onLoadStop: (InAppWebViewController controller, String url) {
                      print(url);
                    },
                    shouldOverrideUrlLoading: (InAppWebViewController controller, ShouldOverrideUrlLoadingRequest shouldOverrideUrlLoadingRequest) async {
                      if (Platform.isAndroid || shouldOverrideUrlLoadingRequest.iosWKNavigationType == IOSWKNavigationType.LINK_ACTIVATED) {
                        var url = Uri.parse(shouldOverrideUrlLoadingRequest.url);
                        var queryParams = ((url.hasQuery) ? '&' : '?') + "client=" + (Platform.isAndroid ? 'android': 'ios');
                        var newUrl = shouldOverrideUrlLoadingRequest.url + queryParams;
                        await controller.loadUrl(url: newUrl);
                        return ShouldOverrideUrlLoadingAction.CANCEL;
                      }
                      return ShouldOverrideUrlLoadingAction.ALLOW;
                    },
                  ))
            ])),
      ),
    );
  }
}
Related