iOS技术

iOS 获取手机安装的所有app信息

2023-02-14  本文已影响0人  冰点雨

获取手机上所有的app信息

#pragma mark ––––––––––––– 获取手机上所有的应用
/// iOS12+
- (void)getAppInfoList {
    Class LSApplicationWorkspace = objc_getClass("LSApplicationWorkspace");
    Class LSApplicationProxy = objc_getClass("LSApplicationProxy");

    id defaultWorkspace = [LSApplicationWorkspace performSelector:@selector(defaultWorkspace)];
  
    // 此方法在iOS12+获取不到
//    id allApplications = [defaultWorkspace performSelector:@selector(allInstalledApplications)];
    NSArray *plugins = [defaultWorkspace performSelector:@selector(installedPlugins)];
    
    NSMutableSet *list = [[NSMutableSet alloc] init];
    for (id plugin in plugins) {
        id bundle = [plugin performSelector:@selector(containingBundle)];
        if (bundle) {
            [list addObject:bundle];
        }
    }

    // 遍历所有app信息
    for (id plugin in list) {
        // BundleID
        NSString *bundleIdentifier = [plugin performSelector:@selector(bundleIdentifier)];
        if (![bundleIdentifier containsString:@"com.apple"]) {
            NSLog(@"bundleIdentifier -> %@", bundleIdentifier);
        
        NSString *applicationDSID = [plugin performSelector:@selector(applicationDSID)];
        NSLog(@"applicationDSID -> %@", applicationDSID);

        NSString *applicationIdentifier = [plugin performSelector:@selector(applicationIdentifier)];
        NSLog(@"applicationIdentifier -> %@", applicationIdentifier);

        NSString *applicationType = [plugin performSelector:@selector(applicationType)];
        NSLog(@"applicationType -> %@", applicationType);

        NSString *dynamicDiskUsage = [plugin performSelector:@selector(dynamicDiskUsage)];
        NSLog(@"dynamicDiskUsage -> %@", dynamicDiskUsage);

        NSString *itemID = [plugin performSelector:@selector(itemID)];
        NSLog(@"itemID -> %@", itemID);

        NSString *itemName = [plugin performSelector:@selector(itemName)];
        NSLog(@"itemName -> %@", itemName);

        NSString *minimumSystemVersion = [plugin performSelector:@selector(minimumSystemVersion)];
        NSLog(@"minimumSystemVersion -> %@", minimumSystemVersion);

        NSString *requiredDeviceCapabilities = [plugin performSelector:@selector(requiredDeviceCapabilities)];
        NSLog(@"requiredDeviceCapabilities -> %@", requiredDeviceCapabilities);

        NSString *sdkVersion = [plugin performSelector:@selector(sdkVersion)];
        NSLog(@"sdkVersion -> %@", sdkVersion);

        NSString *shortVersionString = [plugin performSelector:@selector(shortVersionString)];
        NSLog(@"shortVersionString -> %@", shortVersionString);

        NSString *sourceAppIdentifier = [plugin performSelector:@selector(sourceAppIdentifier)];
        NSLog(@"sourceAppIdentifier -> %@", sourceAppIdentifier);

        NSString *staticDiskUsage = [plugin performSelector:@selector(staticDiskUsage)];
        NSLog(@"staticDiskUsage -> %@", staticDiskUsage);

        NSString *teamID = [plugin performSelector:@selector(teamID)];
        NSLog(@"teamID -> %@", teamID);

        NSString *vendorName = [plugin performSelector:@selector(vendorName)];
        NSLog(@"vendorName -> %@", vendorName);
        }
    }
}

/// iOS10 -12,判断是否安装app
- (BOOL)isAppInstalled:(NSString *)bundleId {
    NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
    if ([container load]) {
        Class appContainer = NSClassFromString(@"MCMAppContainer");
        
        id install = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleId];
        if (install) {
            return YES;
        }
    }
    return NO;
}

/// iOS10-
- (void)getAppInfoList10 {
    Class LSApplicationWorkspace = objc_getClass("LSApplicationWorkspace");
    NSObject* workspace = [LSApplicationWorkspace performSelector:@selector(defaultWorkspace)];
    NSArray *allApplications = [workspace performSelector:@selector(allApplications)];

    Class LSApplicationProxy = object_getClass(@"LSApplicationProxy");
    for (LSApplicationProxy in allApplications) {
        // 查看一些信息
        NSString *bundleID = [LSApplicationProxy performSelector:@selector(applicationIdentifier)];
        NSString *version =  [LSApplicationProxy performSelector:@selector(bundleVersion)];
        NSString *shortVersionString =  [LSApplicationProxy performSelector:@selector(shortVersionString)];
        
        NSLog(@"bundleID -> %@", bundleID);
    }
}

根据bundleId打开app

//    YES:可以打开  NO:不可以打开
  BOOL isOpenApp = [self isOpenApp:@"com.xxx.demo"];
    NSLog(@"111======%d",isOpenApp);

//根据bundleID打开app
/// 直接打开某个APP
- (BOOL)isOpenApp:(NSString*)bundleIdentifier {
    Class LSApplicationWorkspace = objc_getClass("LSApplicationWorkspace");
    NSObject *workspace = [LSApplicationWorkspace performSelector:@selector(defaultWorkspace)];
    // 没有安装返回NO
    BOOL isOpenApp = [workspace performSelector:@selector(openApplicationWithBundleID:) withObject:bundleIdentifier];
    return isOpenApp;
}

参考文章:https://www.jianshu.com/p/6b1fa5e5cd5b

上一篇 下一篇

猜你喜欢

热点阅读