Flutter url_launcher unable launch webview short google form link

Viewed 6236

I was using flutter to develop a app with url_launcher 5.7.2 package https://pub.dev/packages/url_launcher that launch webivew only for the google form when user tap some button.

However I find that if using the shorten URL of google form it will hit error of ERR_UNKNOWN_URL_SCHEME, using the original url is working.

I was using this example https://pub.dev/packages/url_launcher/example and replace the url to https://forms.gle/mEwVA8jXmwJEFn5X6 and then click the button of Launch in app(JavaScript ON)

await launch(url,forceSafariVC: true,
        forceWebView: true,
        enableJavaScript: true,);

If using the original long URL https://docs.google.com/forms/d/e/1FAIpQLSfzXnHMRe890CJj5rSxN-jonjrvZ8HvRBSFcdyJD5IDhOr-IQ/viewform?usp=sf_link is working.

I already add android:usesCleartextTraffic="true" to my AndroidManifest.xml but still not working for short url

3 Answers

I suggest you using 'flutter_webview_plugin' package instead of 'url_launcher'.

This problem is occurred by redirecting and browser can not understand starts of 'intent' in android.

enter image description here

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) =>
                  WebViewPage(url: 'https://forms.gle/mEwVA8jXmwJEFn5X6'),
            ),
          );
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return Container();
  }
}

class WebViewPage extends StatefulWidget {
  final String url;
  WebViewPage({Key key, this.url}) : super(key: key);

  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  final flutterWebviewPlugin = FlutterWebviewPlugin();

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

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

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: WebviewScaffold(
        appBar: AppBar(
          title: Text('WebView Page'),
        ),
        url: widget.url,
        userAgent: 'Fake',
        clearCookies: false,
        clearCache: false,
        hidden: true,
        appCacheEnabled: true,
        supportMultipleWindows: true,
      ),
    );
  }
}


Try, this both working on my end. Try to uninstall and then run the application

Code Snippet:

class URLLauncher extends StatelessWidget {
  Future<void> _launchInBrowser(String url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
        forceSafariVC: false,
        forceWebView: false,
        headers: <String, String>{'my_header_key': 'my_header_value'},
      );
    } else {
      throw 'Could not launch $url';
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: RaisedButton(onPressed: () {
        // _launchInBrowser("https://docs.google.com/forms/d/e/1FAIpQLSfzXnHMRe890CJj5rSxN-jonjrvZ8HvRBSFcdyJD5IDhOr-IQ/viewform?usp=sf_link ");
        _launchInBrowser("https://forms.gle/mEwVA8jXmwJEFn5X6");
      }),
    );
  }
}

when using url launcher we have to follow steps I mention below :-

For Android

Starting from API 30 Android requires package visibility configuration in your AndroidManifest.xml otherwise canLaunch will return false

    <queries>
        <!-- If your app opens https URLs -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
        <!-- If your app makes calls -->
        <intent>
            <action android:name="android.intent.action.DIAL" />
            <data android:scheme="tel" />
        </intent>
        <!-- If your sends SMS messages -->
        <intent>
            <action android:name="android.intent.action.SENDTO" />
            <data android:scheme="smsto" />
        </intent>
        <!-- If your app sends emails -->
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
        </intent>
    </queries>

For Ios

Add any URL schemes passed to canLaunch as LSApplicationQueriesSchemes entries in your Info.plist file.

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>https</string>
  <string>http</string>
</array>
Related