2011-08-24 123 views

回答

11

如果应用程序支持自定义url方案,则可以检查UIApplication-canOpenURL:。这只会告诉你,一个应用程序能够打开该URL方案是可用的,不一定是那个应用程序。没有公开的机制来检查用户在其设备上安装了哪些其他应用程序。

如果您控制这两个应用程序,您可能还使用共享钥匙串或粘贴板更详细地进行通信。

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://”]];' – newenglander

+0

我很抱歉,但是什么应用网址是?它是应用程序的名称还是什么? – ColdSteel

0

为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 
} 
相关问题