5
Linking.openURL(`whatsapp://send?phone=${phoneNumber}`);

The above code will navigate to WhatsApp however I'm wondering if it's possible to check existence of WhatsApp before trying to openURL

Isaac
  • 12,042
  • 16
  • 52
  • 116

2 Answers2

11

From React Native documentation:

To start the corresponding activity for a link (web URL, email, contact etc.), call

Linking.openURL(url).catch(err => console.error('An error occurred', err));

If you want to check if any installed app can handle a given URL beforehand you can call

Linking.canOpenURL(url).then(supported => {
  if (!supported) {
    console.log('Can\'t handle url: ' + url);
  } else {
    return Linking.openURL(url);
  }
}).catch(err => console.error('An error occurred', err));

I assume, if WhatsApp is not installed, the !supported block will be invoked and you can do your computation there.

Ishita Sinha
  • 2,168
  • 4
  • 25
  • 38
2

It can be checked using the PackageManager. Just iterate over the installed packages and compare its name to the whatsapp package name.

PackageManager packageManager = getApplicationContext().getPackageManager();
for (PackageInfo packageInfo : packageManager.getInstalledPackages(0)) {
  if (packageInfo.packageName.equals(“com.whatsapp")) {
    return true;
  }
}
Pol
  • 181
  • 2
  • 10