This article demonstrates how to determine if a user has a specific app installed on their device and set a tag on that information on iOS and Android. |
iOS
Please take the necessary steps to confirm your compliance with Apple's guidelines as you submit to the App Store for approval. Apple has indicated that usage of the canOpenURL method must comply with their privacy requirements. |
To determine whether there is an installed application registered to handle a given URL scheme, use the canOpenURL method in the UIApplication class. The following demonstrates how to use the canOpenURL method to add or remove a tag based on the availability of a URL scheme. This can be useful if you'd like to tailor your message content or message actions based on the URL schemes that a device can launch.
//URL schemes to tag on, must also be declared in plist
BOOL fbInstalled = [self schemeAvailable:@"fb://"];
BOOL yelpInstalled = [self schemeAvailable:@"yelp://"];
BOOL twInstalled = [self schemeAvailable:@"twitter://"];
if (fbInstalled) {
[[UAirship push] addTag:@"can_open_facebook_deeplinks"];
} else {
[[UAirship push] removeTag:@"can_open_facebook_deeplinks"];
}
if (yelpInstalled) {
[[UAirship push] addTag:@"can_open_yelp_deeplinks"];
} else {
[[UAirship push] removeTag:@"can_open_yelp_deeplinks"];
}
[[UAirship push] updateRegistration];
- (BOOL)schemeAvailable:(NSString *) scheme {
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
return [application canOpenURL:URL];
}
For applications running on iOS 9.0 and later, the canOpenUrl method requires a list of URL schemes to be declared in the application's plist. If canOpenURL is called on a scheme that has not been declared it will always return false.
Android
To determine if a specific app is installed, use the PackageManager, a class for retrieving various kinds of information related to the application packages currently installed on the device. To determine if an app is installed on the device you will need to specify the app's package name and use the getPackageInfo method.
Set<String> tags = new HashSet<String>();
tags = UAirship.shared().getPushManager().getTags();
//check to see if the facebook app is installed on the device
boolean installed = isAppInstalled("com.facebook.katana");
if (installed) {
tags.add("facebook-installed");
UAirship.shared().getPushManager().setTags(tags);
} else {
//remove the tag if the app is not installed
tags.remove("facebook-installed");
UAirship.shared().getPushManager().setTags(tags);
}
private boolean isAppInstalled(String uri) {
//Uses Android's PackageManager
PackageManager packageManager = getPackageManager();
boolean app_installed;
try {
packageManager.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
} return app_installed;
}
Related Content: