仅当WhatsApp已安装时才通过WhatsApp分享

8
我正在尝试为已安装应用程序的访问者提供 WhatsApp 分享选项(用于移动网站)。
最好的方法是什么,以验证访问者能够使用此功能,以便我可以相应地启用/禁用它?
该功能将只是使用自定义 URL 方案的链接: Hello, world!

你能解决这个问题吗? - Dhairya Vora
2个回答

10

你可以通过检查链接是否能够打开来解决这个问题。

以下是我的代码

function open_whatsapp(){
    $.ajax({
      type: 'HEAD',
      url: 'whatsapp://send?text=Hello%20World!',
      success: function() {
        window.location='whatsapp://send?text=Hello%20World!';   
      },
      error: function() {
        alert("Whatsapp not installed");
      }
    });
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="share_whatsapp" onclick="open_whatsapp()">Share with Whatsapp</button>


5
每次我执行open_whatsapp()函数时,会出现以下错误:"Failed to load whatsapp://send?phone=+XXX: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https."。 - RaphArbuz

0

这不是一个好的解决方案,因为它取决于操作系统。

if(isMobile()){
   function onWhatsAppClick(e){
       e.preventDefault();
       window.location='whatsapp://send?text=text=Hello%20World!';
   }
}

说明:

  1. 您应该仅在移动设备上检查并显示 WhatsApp
  2. 防止默认行为,并使用 window.location 打开链接。

Plunker:https://plnkr.co/edit/U4CtbxeA81d25lc7dlGe?p=preview


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接