开发者

How to check programmatically if an App is installed?

I am developing an iPhone application which will insta开发者_运维百科ll 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.)


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


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

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


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
}


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>


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>


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.

How to check programmatically if an App is installed?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜