43

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.)

Cœur
  • 37,241
  • 25
  • 195
  • 267
attisof
  • 544
  • 1
  • 5
  • 11
  • 2
    possible duplicate of [how to check installed application in iphone device ](http://stackoverflow.com/questions/3243567/how-to-check-installed-application-in-iphone-device) – Brad Larson Aug 18 '10 at 14:25
  • maybe this wiki will help you too: http://wiki.akosma.com/IPhone_URL_Schemes The problem with most of the url schemes is, you can't pass through any data if you don't have the users id's of the app you're calling – Alex Cio Oct 09 '13 at 19:34
  • http://stackoverflow.com/questions/32643522/fbsdksharedialog-of-facebook-sdk-is-not-working-on-ios9/39159507#39159507 – TharakaNirmana Aug 26 '16 at 06:17

6 Answers6

67

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"]];
}
mvds
  • 45,755
  • 8
  • 102
  • 111
  • 1
    "but if the apps register uri schemes you could test for that" : Could you please explain this slightly further? – attisof Aug 18 '10 at 12:58
30

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

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
    // Facebook app is installed
}
AlBeebe
  • 8,101
  • 3
  • 51
  • 65
28

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
}
Community
  • 1
  • 1
sudo make install
  • 5,629
  • 3
  • 36
  • 48
  • This is really important. Without it, every time you call `canOpenURL()` it will return false and you'll see the log message `This app is not allowed to query for scheme fb`. – s3cur3 Jul 12 '20 at 16:25
7

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>
Vini App
  • 7,339
  • 2
  • 26
  • 43
1

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>
Denis Kreshikhin
  • 8,856
  • 9
  • 52
  • 84
0

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".

D. Pratt
  • 444
  • 8
  • 15