iOS 检测手机中是否安装某个应用
2020-12-08 本文已影响0人
Sinda_Liu
应用场景:
产品要求在APP中检测手机是否安装其他数个APP,并提供了APP对应的bundleID;
网上看了一些,大多分为两种形式:通过URLScheme判断是否可以打开;通过bundleID直接打开APP。第一种方法需要在对应APP的info.plist文件中找scheme,而且有些info.plist文件是找不到scheme的。所以我直接选择了后者。
由于我们的APP不用考虑上架问题,所以就开干吧~
通过bundleID直接打开APP:
+ (BOOL) OpenAppWithBundle:(NSString *)bundleID{
Class LSApplicationWorkspace_class = NSClassFromString([@"LSApplicati" stringByAppendingString:@"onWorkspace"]);
SEL defaultWspc = NSSelectorFromString([@"defaultW" stringByAppendingString:@"orkspace"]);
SEL opensdasadkl = NSSelectorFromString([@"openApplicatio" stringByAppendingString:@"nWithBundleID:"]);
NSObject * workspace = [LSApplicationWorkspace_class performSelector:defaultWspc];
BOOL isopen = [workspace performSelector:opensdasadkl withObject:bundleID];
return isopen;
}
这个方法有一个弊端,就是如果手机里面装有对应的APP,会直接跳转并打开APP。并不满足我的需求。我的不需要跳转,只需要把是否存在的状态通过接口告诉后台即可。so又找了另外一种方式,通过get手机安装的所有APP,遍历查找:
NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
if ([container load]) {
Class appContainer = NSClassFromString(@"MCMAppContainer");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
id container = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleID withObject:nil];
#pragma clang diagnostic pop
NSLog(@"====== ::: %@", [container performSelector:@selector(identifier)]);
if (container) {
return YES;
} else {
return NO;
}
}
return NO;
但是这个方法我试了,目前是获取不到任何APP。所以还是不推荐。最后找到了与第二种方法类似的另外一种形式解决了
通过plugin的形式,直接上代码:
+ (BOOL) verifyAppWithBundle:(NSString *)bundleID{
__block BOOL isInstall = NO;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) {
//iOS12间接获取办法
if ([[UIDevice currentDevice].systemVersion floatValue] >= 12.0){
Class lsawsc = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSArray *plugins = [workspace performSelector:NSSelectorFromString(@"installedPlugins")]; //列出所有plugins
DLOG(@"installedPlugins:%@",plugins);
[plugins enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *pluginID = [obj performSelector:(@selector(pluginIdentifier))];
NSLog(@"pluginID:%@",pluginID);
if([pluginID containsString:bundleID]){
isInstall = YES;
return;
}
}];
return isInstall;
}else{
//iOS11获取办法
NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
if ([container load]) {
Class appContainer = NSClassFromString(@"MCMAppContainer");
id test = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleID withObject:nil];
NSLog(@"%@",test);
if (test) {
return YES;
} else {
return NO;
}
}else{
return NO;
}
}
}else{
//iOS10及以下获取办法
Class lsawsc = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSArray *appList = [workspace performSelector:@selector(allApplications)];
Class LSApplicationProxy_class = object_getClass(@"LSApplicationProxy");
for (LSApplicationProxy_class in appList)
{
//这里可以查看一些信息
NSString *bundleID = [LSApplicationProxy_class performSelector:@selector(applicationIdentifier)];
NSString *version = [LSApplicationProxy_class performSelector:@selector(bundleVersion)];
NSString *shortVersionString = [LSApplicationProxy_class performSelector:@selector(shortVersionString)];
if ([bundleID isEqualToString:bundleID]) {
return YES;
}
}
return NO;
}
return NO;
}