目前获取已安装app的方法主要有以下几种:
1.通过com.apple.mobile.installation.plist文件获取;
2.通过MobileInstallation.framework获取;
3.通过LSApplicationWorkspace:
- #include <objc/runtime.h>
- Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
- NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
- NSLog(@"apps: %@", [workspace performSelector:@selector(allApplications)]);
这种方法可以在非越狱情况下使用,但只能获取应用的bundle ID,无法获取中文名称和icon。
我采用的则是最暴力最直接的方法:递归遍历Applications路径下所有app的plist:
- - (void)scanApps
- {
- NSString *pathOfApplications;
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
- pathOfApplications = @"/var/mobile/Containers/Bundle/Application";
- else
- pathOfApplications = @"/var/mobile/Applications";
- NSLog(@"scan begin");
- // all applications
- NSArray *arrayOfApplications = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathOfApplications error:nil];
- for (NSString *applicationDir in arrayOfApplications) {
- // path of an application
- NSString *pathOfApplication = [pathOfApplications stringByAppendingPathComponent:applicationDir];
- NSArray *arrayOfSubApplication = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathOfApplication error:nil];
- // seek for *.app
- for (NSString *applicationSubDir in arrayOfSubApplication) {
- if ([applicationSubDir hasSuffix:@".app"]) {// *.app
- NSString *path = [pathOfApplication stringByAppendingPathComponent:applicationSubDir];
- NSString *imagePath = [pathOfApplication stringByAppendingPathComponent:applicationSubDir];
- path = [path stringByAppendingPathComponent:@"Info.plist"];
- // so you get the Info.plist in the dict
- NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
- if([[dict allKeys] containsObject:@"CFBundleIdentifier"] && [[dict allKeys] containsObject:@"CFBundleDisplayName"]){
- NSArray *values = [dict allValues];
- <span style="white-space:pre"> </span> NSString *icon;
- <span style="white-space:pre"> </span> for (int i = 0; i < values.count; i++) {
- <span style="white-space:pre"> </span> icon = [self getIcon:[values objectAtIndex:i] withPath:imagePath];
- <span style="white-space:pre"> </span> if (![icon isEqualToString:@""]) { }
- <span style="white-space:pre"> </span> imagePath = [imagePath stringByAppendingPathComponent:icon];
- <span style="white-space:pre"> </span> break;
- <span style="white-space:pre"> </span> }
- <span style="white-space:pre"> </span> }
- }
- }
- }
- }
- }
- - (NSString *)getIcon:(id)value withPath:(NSString *)imagePath
- {
- if([value isKindOfClass:[NSString class]]) {
- NSRange range = [value rangeOfString:@"png"];
- NSRange iconRange = [value rangeOfString:@"icon"];
- NSRange IconRange = [value rangeOfString:@"Icon"];
- if (range.length > 0){
- NSString *path = [imagePath stringByAppendingPathComponent:value];
- UIImage *image = [UIImage imageWithContentsOfFile:path];
- if (image != nil && image.size.width > 50 && image.size.height > 50) {
- return value;
- }
- }
- else if(iconRange.length > 0){
- NSString *imgUrl = [NSString stringWithFormat:@"%@.png",value];
- NSString *path = [imagePath stringByAppendingPathComponent:imgurl];
- UIImage *image = [UIImage imageWithContentsOfFile:path];
- if (image != nil && image.size.width > 50 && image.size.height > 50) {
- return imgUrl<span style="font-family: Arial, Helvetica, sans-serif;">;</span>
- }
- }
- else if(IconRange.length > 0){
- NSString *imgUrl = [NSString stringWithFormat:@"%@.png",value];
- NSString *path = [imagePath stringByAppendingPathComponent:imgurl];
- UIImage *image = [UIImage imageWithContentsOfFile:path];
- if (image != nil && image.size.width > 50 && image.size.height > 50) {
- return imgUrl;
- }
- }
- }
- else if([value isKindOfClass:[NSDictionary class]]){
- NSDictionary *dict = (NSDictionary *)value;
- for (id subValue in [dict allValues]) {
- NSString *str = [self getIcon:subValue withPath:imagePath];
- if (![str isEqualToString:@""]) {
- return str;
- }
- }
- }
- else if([value isKindOfClass:[NSArray class]]){
- for (id subValue in value) {
- NSString *str = [self getIcon:subValue withPath:imagePath];
- if (![str isEqualToString:@""]) {
- return str;
- }
- }
- }
- return @"";
- }