如何以编程方式检查是否安装了应用程序?

时间:2021-02-15 04:07:07

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

我正在开发一个iPhone应用程序,它将在企业中安装很少的第三方应用程序。我有关于捆绑ID的信息。有没有办法使用某些系统API检查应用程序是否已安装?目前,应用程序再次安装,覆盖当前安装。我需要防止这种情况。 (如果已安装该应用程序,Apple的AppStore应用程序将禁用安装选项。)

6 个解决方案

#1


60  

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

我认为这不可能直接,但如果应用程序注册uri方案,你可以测试。

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.

对于facebook app,URI方案例如是fb://。您可以在应用的info.plist中注册。 [UIApplication canOpenURL:url]会告诉您某个网址是否会打开。因此测试fb://是否会打开,将表明已经安装了一个注册了fb://的应用程序 - 这是对facebook应用程序的一个很好的提示。

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

#2


31  

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

这是一个测试Facebook应用程序是否已安装的示例

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

#3


23  

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

对于试图使用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):

首先,您需要通过将以下内容添加到Info.plist文件中来“白名单”URL(安全功能 - 请参阅Leo Natan的回答):

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

#4


4  

Swift 3.1, Swift 3.2, Swift 4

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 :

在Info.plist中添加这些:

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

#5


1  

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

谈到社交网络,最好检查多个方案。 (例如,对于IOS9 SDK,方案'fb'已经过时了。):

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:

当然,Info.plist也应该包含所有必要的方案:

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

#6


0  

iOS 9 and newer update:

iOS 9及更新版本:

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.

由于Apple在iOS 9中进行了一些安全性更改,除了在其他答案中提到的canOpenURL之外,还需要在info.plist中添加带有字符串的LSApplicationQueriesSchemes数组。请参阅截图,了解使用Twitter的示例。

如何以编程方式检查是否安装了应用程序?

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

如果你想添加Facebook,你只需在数组中添加另一个字符串值为“fb”的项目。

#1


60  

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

我认为这不可能直接,但如果应用程序注册uri方案,你可以测试。

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.

对于facebook app,URI方案例如是fb://。您可以在应用的info.plist中注册。 [UIApplication canOpenURL:url]会告诉您某个网址是否会打开。因此测试fb://是否会打开,将表明已经安装了一个注册了fb://的应用程序 - 这是对facebook应用程序的一个很好的提示。

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

#2


31  

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

这是一个测试Facebook应用程序是否已安装的示例

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

#3


23  

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

对于试图使用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):

首先,您需要通过将以下内容添加到Info.plist文件中来“白名单”URL(安全功能 - 请参阅Leo Natan的回答):

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

#4


4  

Swift 3.1, Swift 3.2, Swift 4

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 :

在Info.plist中添加这些:

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

#5


1  

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

谈到社交网络,最好检查多个方案。 (例如,对于IOS9 SDK,方案'fb'已经过时了。):

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:

当然,Info.plist也应该包含所有必要的方案:

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

#6


0  

iOS 9 and newer update:

iOS 9及更新版本:

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.

由于Apple在iOS 9中进行了一些安全性更改,除了在其他答案中提到的canOpenURL之外,还需要在info.plist中添加带有字符串的LSApplicationQueriesSchemes数组。请参阅截图,了解使用Twitter的示例。

如何以编程方式检查是否安装了应用程序?

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

如果你想添加Facebook,你只需在数组中添加另一个字符串值为“fb”的项目。