如何确定iOS设备的用户是否安装了特定的应用程序?如果我知道应用程序的名称,我能否以某种方式使用canOpenURL
?
UIApplication
-canOpenURL:
。这只会告诉您是否有一个能够打开该URL方案的应用程序可用,但不一定是哪个应用程序。没有公开的机制来检查用户在其设备上安装了哪些其他应用程序。
BOOL temp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"yourAppURL://"]];
if(!temp)
{
NSLog(@"INVALID URL"); //Or alert or anything you want to do here
}
针对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")
}
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
}
BOOL temp = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"yourAppURL://"]];
该行代码的意思是检查是否可以打开一个名为"yourAppURL"的应用程序。 - newenglander