在iOS 11 以前我们可以使用LSApplicationWorkspace来获取手机上已安装的应用列表
- (void)getIphoneAllApplications {
ClassLSApplicationWorkspace_class =objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [LSApplicationWorkspace_classperformSelector:@selector(defaultWorkspace)];
NSArray*apps= [workspaceperformSelector:@selector(allApplications)];
ClassLSApplicationProxy_class =objc_getClass("LSApplicationProxy");
for(inti =0; i < apps.count; i++) {
NSObject*temp = apps[i];
if([tempisKindOfClass:LSApplicationProxy_class])
{
//应用的bundleId
NSString*appBundleId = [tempperformSelector:NSSelectorFromString(@"applicationIdentifier")];
//应用的名称
NSString *appName = [temp performSelector:NSSelectorFromString(@"localizedName")];
//应用的类型是系统的应用还是第三方的应用
NSString * type = [temp performSelector:NSSelectorFromString(@"applicationType")];
//应用的版本
NSString* shortVersionString = [tempperformSelector:NSSelectorFromString(@"shortVersionString")];
NSString* resourcesDirectoryURL = [tempperformSelector:NSSelectorFromString(@"containerURL")];
NSLog(@"类型=%@应用的BundleId=%@ ++++应用的名称=%@版本号=%@\n%@",type,appBundleId,appName,shortVersionString,resourcesDirectoryURL);
}
}
}
iOS 11 上获取所有已安装应用接口被禁,但可以根据BundleId检查App是否存在
- (BOOL)isInstalled:(NSString *)bundleId {
NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
if ([container load]) {
Class appContainer = NSClassFromString(@"MCMAppContainer");#pragmaclang diagnostic push#pragmaclang diagnostic ignored "-Wundeclared-selector"idcontainer = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleId withObject:nil];#pragmaclang diagnostic pop NSLog(@"%@", [container performSelector:@selector(identifier)]);
if (container) {
return YES;
} else {
return NO;
}
}
return NO;
}