【问题标题】:I want to launch WhatsApp application from my flutter application我想从我的颤振应用程序启动 WhatsApp 应用程序
【发布时间】:2020-11-28 10:09:33
【问题描述】:

我在我的项目中使用这个依赖 url_launcher: ^5.4.1 通过我的颤振应用程序启动 whatsapp 但是当我按下按钮启动应用程序时它不起作用但在模拟器上显示无法打开链接的错误消息. 下面给出的是我用来启动whatsapp的函数代码。

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';


void main() => runApp(Wapp());

class Wapp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
          primarySwatch: Colors.orange,
      ),
      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> {
 void launchWhatsApp(
    {@required int phone,
    @required String message,
    }) async {
  String url() {
    if (Platform.isAndroid) {
      return "whatsapp://wa.me/$phone:03452121308:/?text=${Uri.parse(message)}";
    } else {
      return "whatsapp://send?   phone=$phone&text=${Uri.parse(message)}";
    }
  }

  if (await canLaunch(url())) {
    await launch(url());
  } else {
    throw 'Could not launch ${url()}';
  }
}


Widget build(BuildContext context){
  return Scaffold(
    appBar: AppBar(
      title: Text("Home"), 
    ),

  body: Center(
    child: RaisedButton(
      
      color: Colors.orange,
      textColor: Colors.black,
      padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0),
      highlightColor: Colors.green,
      onPressed: () {
        launchWhatsApp(phone: 03452121308, message: 'Hello');
      },
      child: Text("Place Your Order",style: TextStyle(
          
          fontWeight: FontWeight.bold,
          fontSize: 15
          
        )
      )
    )
  )

  );
}

}

【问题讨论】:

    标签: android flutter dart whatsapp launcher


    【解决方案1】:

    我找到了将此代码添加到 android/app/src/main/AndroidManifest.xml 的解决方案

    <queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.intent.action.DIAL" />
        <data android:scheme="tel" />
    </intent>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
    

    【讨论】:

      【解决方案2】:
       String url() {
            if (Platform.isIOS) {
              return "whatsapp://wa.me/$phone/?text=${Uri.encodeFull(message)}";
            } else {
              return "whatsapp://send?phone=$phone&text=${Uri.encodeFull(message)}";
            }
          }
      

      【讨论】:

      【解决方案3】:

      您的url 中缺少https://

      用您的url() 方法替换下面的代码:

        String url() {
          if (Platform.isAndroid) {
            // add the [https]
            return "https://wa.me/$phone/?text=${Uri.parse(message)}"; // new line
          } else {
            // add the [https]
            return "https://api.whatsapp.com/send?phone=$phone=${Uri.parse(message)}"; // new line
          }
        }
      

      【讨论】:

      • 添加 https 后仍然无法正常工作,显示无法访问网站。此外,我想启动应用程序而不是网站
      • 如果您尝试发送消息,请考虑使用:url https://wa.me/$phone/?text=${Uri.parse(message)}。您提供了错误的网址。我更新了我的答案以使用正确的网址。
      • 是的,它现在正在工作,但我将如何打开与特定联系人的聊天
      • 只要你给它一个正确的电话,上面的代码就会打开一个特定的联系人。 @MoizKhalil。我不知道你还要求什么。
      • @void 我试过你的代码..它适用于所有 iPhone 设备以及一些低端 Andriod 设备..但对于所有高规格的 Andriod 设备,WhatsApp 没有打开..(没有显示错误.. 甚至无法点击...)我必须在 Andriod.xml 文件中进行一些更改吗?
      猜你喜欢
      • 2019-10-11
      • 2021-07-30
      • 2021-08-23
      • 2021-03-26
      • 2020-05-11
      • 1970-01-01
      • 2022-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多