iOS 全息备份研究

##一、获取手机中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的图标。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值