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

本文介绍了一种非越狱环境下获取iOS设备上已安装应用的详细信息的方法,包括应用的bundleID、中文名称及图标。同时分享了一个递归遍历Applications路径下所有app的plist来获取这些信息的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


目前获取已安装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 @"";
}



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值