Isaac
Isaac

Reputation: 12874

How to check if WhatsApp is installed on device?

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

Upvotes: 5

Views: 8716

Answers (2)

Ishita Sinha
Ishita Sinha

Reputation: 2294

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.

Upvotes: 11

Pol
Pol

Reputation: 181

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

Upvotes: 2

Related Questions