iOS11 获取手机已安装应用列表

在iOS 11 以前我们可以使用LSApplicationWorkspace来获取手机上已安装的应用列表

iOS 11 上获取所有已安装应用接口被禁,但可以根据BundleId检查App是否存在

- (BOOL)isInstalled:(NSString *)bundleId {
    NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
    if ([container load]) {
        Class appContainer = NSClassFromString(@"MCMAppContainer");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
        id container = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleId withObject:nil];
#pragma clang diagnostic pop
        NSLog(@"%@", [container performSelector:@selector(identifier)]);
        if (container) {
            return YES;
        } else {
            return NO;
        }
    }
    return NO;
}

此方法在iOS8中不起作用,经笔者验证,此方法在iOS9以上系统可正确运行。

 

老方法LSApplicationWorkspace,经笔者验证在iOS8 ~ iOS10均可正常获取所有已安应用

附老方法代码

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
    SEL selector    =   NSSelectorFromString(@"defaultWorkspace");
    NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
    SEL selectorALL = NSSelectorFromString(@"allInstalledApplications");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    NSArray *allInstallApps = [workspace performSelector:selectorALL];
    
    for (NSObject *obj in allInstallApps) {
        NSString *appBundleId   = [obj performSelector:@selector(applicationIdentifier)];
        NSLog(@"installed %@", appBundleId);
    }
#pragma clang diagnostic pop    // -Wundeclared-selector
#pragma clang diagnostic pop    // -Warc-performSelector-leaks

 

更多私有API探索,感兴趣的同学可用此工具浏览,不幸的是貌似在iOS11上不能完美运行

posted @ 2018-06-25 16:19  Coding&Life  阅读(6262)  评论(0编辑  收藏  举报