attisof
attisof

Reputation: 554

How to check programmatically if an App is installed?

I am developing an iPhone application which will install few third party applications in an enterprise. I have the information about the bundle IDs. Is there a way to check if the application is already installed, using some system APIs? Currently the application gets installed again, overwriting the current installation. I need to prevent this some how. (Apple's AppStore application disables the installation option if the app is already installed.)

Upvotes: 43

Views: 52093

Answers (6)

D. Pratt
D. Pratt

Reputation: 464

iOS 9 and newer update:

Because of some security changes Apple made starting in iOS 9, in addition to canOpenURL mentioned in other answers, you also need to add an LSApplicationQueriesSchemes array with strings in info.plist. See screenshot for an example using Twitter.

info.plist LSApplicationQueriesSchemes array

If you wanted to add Facebook, you would just add another item in the array with a string value of "fb".

Upvotes: 0

Vini App
Vini App

Reputation: 7485

Swift 3.1, Swift 3.2, Swift 4

if let urlFromStr = URL(string: "fb://") {
    if UIApplication.shared.canOpenURL(urlFromStr) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(urlFromStr, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(urlFromStr)
        }
    }
}

Add these in Info.plist :

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fb</string>
</array>

Upvotes: 7

Denis Kreshikhin
Denis Kreshikhin

Reputation: 9410

When it comes to social networks, it is best to check multiple schemes. (Because scheme 'fb' is obsolete for IOS9 SDK for example.):

NSArray* fbSchemes = @[
     @"fbapi://", @"fb-messenger-api://", @"fbauth2://", @"fbshareextension://"];
BOOL isInstalled = false;

for (NSString* fbScheme in fbSchemes) {
    isInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:fbScheme]];
    if(isInstalled) break;
}

if (!isInstalled) {
    // code
    return;
}

Of course Info.plist also should contain all necessary schemes:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
</array>

Upvotes: 1

sudo make install
sudo make install

Reputation: 5669

For anyone trying to do this with iOS 9/Swift 2:

First, you'll need to 'whitelist' the URL by adding the following to your Info.plist file (a security feature--see Leo Natan's answer):

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fb</string>
</array>

After that, you can ask whether the app is available and has a registered scheme:

guard UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb://")!) else {
    NSLog("No Facebook?  You're a better man than I am, Charlie Brown.")
    return
}

Upvotes: 28

mvds
mvds

Reputation: 47034

I think this is not possible directly, but if the apps register uri schemes you could test for that.

A URI scheme is for example fb:// for the facebook app. You can register that in the info.plist of your app. [UIApplication canOpenURL:url] will tell you if a certain url will or will not open. So testing if fb:// will open, will indicate that there is an app installed which registered fb:// - which is a good hint for the facebook app.

// check whether facebook is (likely to be) installed or not
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
    // Safe to launch the facebook app
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"fb://profile/200538917420"]];
}

Upvotes: 67

AlBeebe
AlBeebe

Reputation: 8121

Here's an example to test if the facebook app is installed

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
    // Facebook app is installed
}

Upvotes: 30

Related Questions