如何确定用户是否安装了iOS应用程序?

11

如何确定iOS设备的用户是否安装了特定的应用程序?如果我知道应用程序的名称,我能否以某种方式使用canOpenURL

4个回答

12
如果应用支持自定义URL方案,您可以检查 UIApplication -canOpenURL:。这只会告诉您是否有一个能够打开该URL方案的应用程序可用,但不一定是哪个应用程序。没有公开的机制来检查用户在其设备上安装了哪些其他应用程序。
如果您控制两个应用程序,您还可以使用共享的keychain或粘贴板来进行更详细地通信。

8
你也可以这样检查:

BOOL temp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"yourAppURL://"]];
            
if(!temp)
{
    NSLog(@"INVALID URL"); //Or alert or anything you want to do here
}

3
第一行代码在我的电脑上无法编译,但这行可以: BOOL temp = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"yourAppURL://"]]; 该行代码的意思是检查是否可以打开一个名为"yourAppURL"的应用程序。 - newenglander
很抱歉,但是应用程序的URL是什么?它是应用程序的名称还是其他什么东西? - Coldsteel48

1

针对Swift用户

     let urlPath: String = "fb://www.facebook.com"
     let url: NSURL = NSURL(string: urlPath)!

    let isInstalled = UIApplication.sharedApplication().canOpenURL(url)
    if isInstalled {
        print("Installed")
    }else{
        print("Not installed")
    }

0

Facebook在内部使用https://github.com/facebook/facebook-ios-sdk/blob/master/FBSDKCoreKit/FBSDKCoreKit/Internal/FBSDKInternalUtility.m,你也可以这样做

#define FBSDK_CANOPENURL_FACEBOOK @"fbauth2"

+ (BOOL)isFacebookAppInstalled
{
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    [FBSDKInternalUtility checkRegisteredCanOpenURLScheme:FBSDK_CANOPENURL_FACEBOOK];
  });
  NSURLComponents *components = [[NSURLComponents alloc] init];
  components.scheme = FBSDK_CANOPENURL_FACEBOOK;
  components.path = @"/";
  return [[UIApplication sharedApplication]
          canOpenURL:components.URL];
}

使用Swift 3编写的代码

static func isFacebookAppInstalled() -> Bool {
  let schemes = ["fbauth2", "fbapi", "fb"]
  let schemeUrls = schemes.flatMap({ URL(string: "\($0)://") })

  return !schemeUrls.filter({ UIApplication.shared.canOpenURL($0) }).isEmpty
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接