Flutter url_launcher 无法启动 webview short google form link

Flutter url_launcher unable launch webview short google form link

我正在使用 flutter 开发一个带有 url_launcher 5.7.2 包 https://pub.dev/packages/url_launcher 的应用程序,当用户点击某个按钮时,它仅针对 google 表单启动 webivew。

但是我发现如果使用 google 形式的缩短 URL 会遇到 ERR_UNKNOWN_URL_SCHEME 的错误,使用原始 url 是可行的。

我正在使用此示例 https://pub.dev/packages/url_launcher/example 并将 url 替换为 https://forms.gle/mEwVA8jXmwJEFn5X6 然后单击 在应用程序中启动 (JavaScript ON)

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

如果使用原始 long URL https://docs.google.com/forms/d/e/1FAIpQLSfzXnHMRe890CJj5rSxN-jonjrvZ8HvRBSFcdyJD5IDhOr-IQ/viewform?usp=sf_link 是可行的。

我已经将 android:usesCleartextTraffic="true" 添加到我的 AndroidManifest.xml,但仍然无法正常工作 url

我建议您使用 'flutter_webview_plugin' 包而不是 'url_launcher'。

此问题是通过重定向发生的,浏览器无法理解 android 中 'intent' 的开头。

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,
      ),
    );
  }
}


试试看,这两个都对我有用。尝试卸载然后 运行 应用程序

代码段:

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");
      }),
    );
  }
}

使用 url 启动器时,我们必须按照我在下面提到的步骤进行操作 :-

对于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>

对于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>