##一、获取手机中app列表
###1、获取iPhone中所有安装的APP
获取iPhone手机中安装的所有App苹果没有提供直接的方法,但是可以通过苹果私有的api获取到。代码如下:
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
// 获取已安装app的列表
NSArray *appList = [workspace performSelector:@selector(allApplications)];
###2、获取app相关信息
获取app一些相关的信息,比如名称、版本、图标、bundle Id 等。有两种方式可以获取。
1、通过应用资源文件中的plist文件获取
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
// 获取已安装app的列表
NSArray *appList = [workspace performSelector:@selector(allApplications)];
// 开始遍历appList数组中每个LSApplicationProxy类型的对象
[appList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
ProgramModel *appModel = [[ProgramModel alloc] init];
NSObject *object = appList[idx];
//获取应用资源文件路径
NSString *filePath = [[object performSelector:@selector(resourcesDirectoryURL)] path];
//获取plist文件的路径
NSString *plistPath = [filePath stringByAppendingString:@"/info.plist"];
NSDictionary *dictInfo = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(@"%@",dictInfo);
NSString *name = dictInfo[@"CFBundleName"];
NSString *bundleId = dictInfo[@"CFBundleIdentifier"];
NSString *version = dictInfo[@"CFBundleVersion"];
NSArray *arrIcon = dictInfo[@"CFBundleIconFiles"];
NSString *icon;
if (arrIcon.count > 0) {
icon = dictInfo[@"CFBundleIconFiles"][0];
}
NSString *imagePath = [NSString stringWithFormat:@"%@/%@.png",filePath,icon];
appModel.appName = name;
appModel.appVersion = version;
appModel.appBundleId = bundleId;
appModel.appIcon = imagePath;
[arrData addObject:appModel];
这种方法,能够获取大部分app的名称和小部分app的图标。为什么有些app的名称和图标获取不到,是因为有些应用的plist文件中没有这些数据。经过测试,这种方式只能在模拟器上获取到,真机上获取不到。
2、通过私有的API获取APP相关信息
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
// 获取已安装app的列表
NSArray *appList = [workspace performSelector:@selector(allApplications)];
// 开始遍历appList数组中每个LSApplicationProxy类型的对象
[appList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
ProgramModel *appModel = [[ProgramModel alloc] init];
NSObject *object = appList[idx];
NSString *tempKey = [object performSelector:NSSelectorFromString(@"applicationIdentifier")];
if (![tempKey containsString:@"apple"]) {
//版本
NSString *version = [object performSelector:NSSelectorFromString(@"shortVersionString")];
//Bundle id
NSString *bundleId = [object performSelector:NSSelectorFromString(@"applicationIdentifier")];
// 下面两句代码是重点:利用私有API获取app的图标的路径
NSDictionary *dict = [object performSelector:@selector(boundIconsDictionary)];
NSString *appIconPath = [NSString stringWithFormat:@"%@/%@.png",
[[object performSelector:@selector(resourcesDirectoryURL)] path],
[[[dict objectForKey:@"CFBundlePrimaryIcon"] objectForKey:@"CFBundleIconFiles"] lastObject]];
appModel.appVersion = version;
appModel.appBundleId = bundleId;
appModel.appIcon = appIconPath;
[arrData addObject:appModel];
}
}];
这种方法可以在模拟器和真机上都有效,但是获取不到APP的名称,能够获取部分APP的图标。