越狱状态下获取设备上已安装的app和icon

目前获取已安装app的方法主要有以下几种:

1.通过com.apple.mobile.installation.plist文件获取;

2.通过MobileInstallation.framework获取;

3.通过LSApplicationWorkspace:

[html]  view plain  copy
  1. #include <objc/runtime.h>  
  2. Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");  
  3. NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];  
  4. NSLog(@"apps: %@", [workspace performSelector:@selector(allApplications)]);  

这种方法可以在非越狱情况下使用,但只能获取应用的bundle ID,无法获取中文名称和icon。


我采用的则是最暴力最直接的方法:递归遍历Applications路径下所有app的plist:

[objc]  view plain  copy
  1. - (void)scanApps  
  2. {  
  3.     NSString *pathOfApplications;  
  4.     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)  
  5.         pathOfApplications = @"/var/mobile/Containers/Bundle/Application";  
  6.     else  
  7.         pathOfApplications = @"/var/mobile/Applications";  
  8.       
  9.     NSLog(@"scan begin");  
  10.       
  11.     // all applications  
  12.     NSArray *arrayOfApplications = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathOfApplications error:nil];  
  13.       
  14.     for (NSString *applicationDir in arrayOfApplications) {  
  15.         // path of an application  
  16.         NSString *pathOfApplication = [pathOfApplications stringByAppendingPathComponent:applicationDir];  
  17.         NSArray *arrayOfSubApplication = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathOfApplication error:nil];  
  18.         // seek for *.app  
  19.         for (NSString *applicationSubDir in arrayOfSubApplication) {  
  20.             if ([applicationSubDir hasSuffix:@".app"]) {// *.app  
  21.                 NSString *path = [pathOfApplication stringByAppendingPathComponent:applicationSubDir];  
  22.                 NSString *imagePath = [pathOfApplication stringByAppendingPathComponent:applicationSubDir];  
  23.                 path = [path stringByAppendingPathComponent:@"Info.plist"];  
  24.                 // so you get the Info.plist in the dict  
  25.                 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];  
  26.                 if([[dict allKeys] containsObject:@"CFBundleIdentifier"] && [[dict allKeys] containsObject:@"CFBundleDisplayName"]){  
  27.                     NSArray *values = [dict allValues];  
  28.         <span style="white-space:pre">    </span>    NSString *icon;  
  29.         <span style="white-space:pre">    </span>    for (int i = 0; i < values.count; i++) {  
  30.            <span style="white-space:pre">       </span> icon = [self getIcon:[values objectAtIndex:i] withPath:imagePath];  
  31.            <span style="white-space:pre">       </span> if (![icon isEqualToString:@""]) {                      }  
  32.                 <span style="white-space:pre">   </span>     imagePath = [imagePath stringByAppendingPathComponent:icon];  
  33.             <span style="white-space:pre">      </span>     break;  
  34.            <span style="white-space:pre">       </span> }  
  35.        <span style="white-space:pre">     </span>    }  
  36.                 }  
  37.             }  
  38.         }  
  39.     }  
  40. }  
[objc]  view plain  copy
  1. - (NSString *)getIcon:(id)value withPath:(NSString *)imagePath  
  2. {  
  3.     if([value isKindOfClass:[NSString class]]) {  
  4.         NSRange range = [value rangeOfString:@"png"];  
  5.         NSRange iconRange = [value rangeOfString:@"icon"];  
  6.         NSRange IconRange = [value rangeOfString:@"Icon"];  
  7.         if (range.length > 0){  
  8.             NSString *path = [imagePath stringByAppendingPathComponent:value];  
  9.             UIImage *image = [UIImage imageWithContentsOfFile:path];  
  10.             if (image != nil && image.size.width > 50 && image.size.height > 50) {  
  11.                 return value;  
  12.             }  
  13.         }  
  14.         else if(iconRange.length > 0){  
  15.             NSString *imgUrl = [NSString stringWithFormat:@"%@.png",value];  
  16.             NSString *path = [imagePath stringByAppendingPathComponent:imgurl];  
  17.             UIImage *image = [UIImage imageWithContentsOfFile:path];  
  18.             if (image != nil && image.size.width > 50 && image.size.height > 50) {  
  19.                 return imgUrl<span style="font-family: Arial, Helvetica, sans-serif;">;</span>  
  20.             }  
  21.         }  
  22.         else if(IconRange.length > 0){  
  23.             NSString *imgUrl = [NSString stringWithFormat:@"%@.png",value];  
  24.             NSString *path = [imagePath stringByAppendingPathComponent:imgurl];  
  25.             UIImage *image = [UIImage imageWithContentsOfFile:path];  
  26.             if (image != nil && image.size.width > 50 && image.size.height > 50) {  
  27.                 return imgUrl;  
  28.             }  
  29.         }  
  30.     }  
  31.     else if([value isKindOfClass:[NSDictionary class]]){  
  32.         NSDictionary *dict = (NSDictionary *)value;  
  33.         for (id subValue in [dict allValues]) {  
  34.             NSString *str = [self getIcon:subValue withPath:imagePath];  
  35.             if (![str isEqualToString:@""]) {  
  36.                 return str;  
  37.             }  
  38.         }  
  39.     }  
  40.     else if([value isKindOfClass:[NSArray class]]){  
  41.         for (id subValue in value) {  
  42.             NSString *str = [self getIcon:subValue withPath:imagePath];  
  43.             if (![str isEqualToString:@""]) {  
  44.                 return str;  
  45.             }  
  46.         }  
  47.     }  
  48.     return @"";  
  49. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值