无法启动 'URL',因为该方案没有已注册的处理程序

Failed to launch 'URL' because the scheme does not have a registered handler

我正在尝试使用 angular 11 和 ionic 5 构建社交分享组件。我正在使用锚标记来调用 href="whatsapp://send?#text=some%20text ”。这在安装了 WhatsApp 的设备上工作正常,但我只在没有安装 WhatsApp 的设备上的浏览器控制台中收到以下错误:

无法启动'whatsapp://send?#text=text=some%20text',因为该方案没有已注册的处理程序。

我如何捕获此错误以向用户显示一条友好的消息,例如“抱歉,您没有安装 WhatsApp”

直接用href属性好像不行

但是,如果您将此逻辑移动到组件中,则有多种选择

内部应用程序:

您可以使用 this plugin 查看应用可用性,即

let app;

if (this.platform.is('ios')) {
  app = 'twitter://';
} else if (this.platform.is('android')) {
  app = 'com.twitter.android';
}

this.appAvailability.check(app)
  .then(
    (yes: boolean) => console.log(app + ' is available'),
    (no: boolean) => console.log(app + ' is NOT available')
  );

浏览器内:

  1. 使用超时回退,即

    <!-- Deep link URL for existing users with app already installed on their device -->
    window.location = 'yourapp://app.com/?screen=xxxxx';
    
    <!-- Download URL (TUNE link) for new users to download the app -->
    setTimeout("window.location = 'http://hastrk.com/serve?action=click&publisher_id=1&site_id=2';", 1000);
    

    实际上,这是我们在我们的一个网络应用程序中使用的方式,并且它成功运行。

  2. 使用深层链接handler library,这样您就可以像这样使用深层链接

    <a href           ="..."  Fallback  (and unsupported OSs)
       data-app       ="..."  Deep link (cross-OS)
       data-app-[os]  ="..."  Deep link (OS-specific)
       data-store-[os]="..."> Store ID  (OS-specific)
    

    我以前没用过所以说不出有什么特别之处

我用它来检查应用程序是否已安装。

 "intent://scan/#Intent;scheme=whatsapp://send?#text=text=some%20text;S.browser_fallback_url=https://play.google.com/store/apps/details?id=com.whatsapp;end"