How to check if WhatsApp is installed on device?

Viewed 6582
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

2 Answers

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.

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;
  }
}
Related