如何确定用户是否安装了iOS应用程序?
答
如果应用程序支持自定义url方案,则可以检查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://”]];' – newenglander
我很抱歉,但是什么应用网址是?它是应用程序的名称还是什么? – ColdSteel