zoukankan      html  css  js  c++  java
  • 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上不能完美运行

  • 相关阅读:
    systick运用
    stm32的systick原理与应用
    PID算法知识点博文收藏记录
    关于STM32驱动DS1302实时时钟的一点思考
    什么是同步?什么是互斥?
    C语言小笔记(1)
    typedef 复杂函数指针
    获取窗口句柄
    GetWindowRect和GetClientRect的区别详解
    RepositionBars的用法和参数的意义(引用别人的)
  • 原文地址:https://www.cnblogs.com/ciml/p/9224826.html
Copyright © 2011-2022 走看看