#import <objc/runtime.h>
- (void)installedApplications
{
Class lsawsc = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSArray *apps = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];
Class LSApplicationProxy_class = objc_getClass("LSApplicationProxy");
for (int i = 0; i < apps.count; i++) {
NSObject *temp = apps[i];
if ([temp isKindOfClass:LSApplicationProxy_class]) {
//应用的bundleId
NSString *appBundleId = [temp performSelector:NSSelectorFromString(@"applicationIdentifier")];
//应用的名称
NSString *appName = [temp performSelector:NSSelectorFromString(@"localizedName")];
//应用的类型是系统的应用还是第三方的应用
NSString * type = [temp performSelector:NSSelectorFromString(@"applicationType")];
//应用的版本
NSString * shortVersionString = [temp performSelector:NSSelectorFromString(@"shortVersionString")];
NSString * resourcesDirectoryURL = [temp performSelector:NSSelectorFromString(@"containerURL")];
NSLog(@"类型=%@应用的BundleId=%@ ++++应用的名称=%@版本号=%@\n%@",type,appBundleId,appName,shortVersionString,resourcesDirectoryURL);
}
}
}
//调用App
- (void)openApp {
PrivateApi_LSApplicationWorkspace* workspace = [NSClassFromString(@"LSApplicationWorkspace") new];
[workspace openApplicationWithBundleID:@"com.xxx.xxx"];
}
//获取所有App包名
1.获取到手机里面所有的APP包名
- (void)touss
{
Class lsawsc = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSArray *Arr = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];
for (NSString * tmp in Arr)
{
NSString * bundleid = [self getParseBundleIdString:tmp];
NSLog(@"%@",bundleid);
}
}
- (NSString *)getParseBundleIdString:(NSString *)description
{
NSString * ret = @"";
NSString * target = [description description];
// iOS8.0 "LSApplicationProxy: com.apple.videos",
// iOS8.1 "<LSApplicationProxy: 0x898787998> com.apple.videos",
// iOS9.0 "<LSApplicationProxy: 0x145efbb0> com.apple.PhotosViewService <file:///Applications/PhotosViewService.app>"
if (target == nil) {
return ret;
}
NSArray * arrObj = [target componentsSeparatedByString:@" "];
switch ([arrObj count]) {
case 2: // [iOS7.0 ~ iOS8.1)
case 3: {
// [iOS8.1 ~ iOS9.0)
ret = [arrObj lastObject];
}
break;
case 4: {
// [iOS9 +)
ret = [arrObj objectAtIndex:2];
}
break;
default:
break;
}
return ret;
}
2.通过包名去打开应用
Class lsawsc = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
// iOS6 没有defaultWorkspace
if ([workspace respondsToSelector:NSSelectorFromString(@"openApplicationWithBundleID:")])
{
[workspace performSelector:NSSelectorFromString(@"openApplicationWithBundleID:") withObject:@"com.Calendar.jbp"];
}
更新 来源()
在iOS 11 以前我们可以使用LSApplicationWorkspace来获取手机上已安装的应用列表
iOS 11 上获取所有已安装应用接口被禁,但可以根据BundleId检查App是否存在
- (BOOL)isInstalled:(NSString *)bundleId {
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;
}
此方法在iOS8中不起作用,经笔者验证,此方法在iOS9以上系统可正确运行。
2020年02月27更新
企业版安装和 测试的APP 没搜到信息
id space = [NSClassFromString(@"LSApplicationWorkspace") performSelector:@selector(defaultWorkspace)];
NSArray *plugins = [space performSelector:@selector(installedPlugins)];
NSMutableSet *list = [[NSMutableSet alloc] init];
for (id plugin in plugins) {
id bundle = [plugin performSelector:@selector(containingBundle)];
if (bundle)
[list addObject:bundle];
}
int a = 1;
for (id plugin in list) {
NSLog(@"🐒 %d--",a);
a++;
NSLog(@"bundleIdentifier =%@", [plugin performSelector:@selector(bundleIdentifier)]);//bundleID
NSLog(@"applicationDSID =%@", [plugin performSelector:@selector(applicationDSID)]);
NSLog(@"applicationIdentifier =%@", [plugin performSelector:@selector(applicationIdentifier)]);
NSLog(@"applicationType =%@", [plugin performSelector:@selector(applicationType)]);
NSLog(@"dynamicDiskUsage =%@", [plugin performSelector:@selector(dynamicDiskUsage)]);
NSLog(@"itemID =%@", [plugin performSelector:@selector(itemID)]);
NSLog(@"itemName =%@", [plugin performSelector:@selector(itemName)]);
NSLog(@"minimumSystemVersion =%@", [plugin performSelector:@selector(minimumSystemVersion)]);
NSLog(@"requiredDeviceCapabilities =%@", [plugin performSelector:@selector(requiredDeviceCapabilities)]);
NSLog(@"sdkVersion =%@", [plugin performSelector:@selector(sdkVersion)]);
NSLog(@"shortVersionString =%@", [plugin performSelector:@selector(shortVersionString)]);
NSLog(@"sourceAppIdentifier =%@", [plugin performSelector:@selector(sourceAppIdentifier)]);
NSLog(@"staticDiskUsage =%@", [plugin performSelector:@selector(staticDiskUsage)]);
NSLog(@"teamID =%@", [plugin performSelector:@selector(teamID)]);
NSLog(@"vendorName =%@", [plugin performSelector:@selector(vendorName)]);
}
return;
显示一个app的信息
只显示一个app的信息
bundleIdentifier =com.mobike.bike
applicationDSID =8124785344
applicationIdentifier =com.mobike.bike
applicationType =User
dynamicDiskUsage =67100672
itemID =1044535426
itemName =摩拜单车-好骑可靠的共享单车
minimumSystemVersion =12.0
requiredDeviceCapabilities =(
sdkVersion =12.1
shortVersionString =8.12.1
sourceAppIdentifier =(null)
staticDiskUsage =88735744
teamID =2M4WW2KKY5
vendorName =Beijing Mobike Technology Co., Ltd.
慢慢来,一步一个巴掌印~~~