代码之家  ›  专栏  ›  技术社区  ›  attisof

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

  •  37
  • attisof  · 技术社区  · 14 年前

    我正在开发一个iPhone应用程序,它将在企业中安装很少的第三方应用程序。我有关于包ID的信息。有没有办法使用一些系统api检查应用程序是否已经安装?当前,应用程序将再次安装,覆盖当前安装。我需要怎样才能阻止这一切(如果应用程序已经安装,苹果的AppStore应用程序将禁用安装选项。)

    6 回复  |  直到 7 年前
        1
  •  66
  •   mvds    11 年前

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

    例如,URI方案 fb:// [UIApplication canOpenURL:url] 会告诉你某个网址会不会打开。所以测试一下 将打开,表示有一个已注册的应用程序已安装 餐饮部:// -这对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
  •  30
  •   AlBeebe    13 年前

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {
        // Facebook app is installed
    }
    
        3
  •  27
  •   Community leo1    7 年前

    对于任何试图通过iOS 9/Swift 2做到这一点的人:

    首先,您需要将以下内容添加到您的 Info.plist Leo Natan's answer ):

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

    guard UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb://")!) else {
        NSLog("No Facebook?  You're a better man than I am, Charlie Brown.")
        return
    }
    
        4
  •  7
  •   Vini App    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)
            }
        }
    }
    

    把这些加进去Info.plist :

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fb</string>
    </array>
    
        5
  •  1
  •   Denis Kreshikhin    8 年前

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

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

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
    </array>
    
        6
  •  0
  •   D. Pratt    6 年前

    由于苹果从ios9开始进行了一些安全更改,除了其他答案中提到的canOpenURL之外,还需要添加一个lsapplicationqueryesschemes数组,其中包含字符串info.plist. 有关使用Twitter的示例,请参见屏幕截图。

    info.plist LSApplicationQueriesSchemes array

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