美文网首页
iOS 获取手机安装的所有的Apps

iOS 获取手机安装的所有的Apps

作者: iLeooooo | 来源:发表于2017-11-02 10:58 被阅读1425次
    #import <objc/runtime.h>
    
    - (void)installedApplications
    {
        Class lsawsc = objc_getClass("LSApplicationWorkspace");
        NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
        NSArray *apps = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];
    
    
        Class LSApplicationProxy_class = objc_getClass("LSApplicationProxy");
        for (int i = 0; i < apps.count; i++) {
            NSObject *temp = apps[i];
            if ([temp isKindOfClass:LSApplicationProxy_class]) {
                //应用的bundleId
                NSString *appBundleId = [temp performSelector:NSSelectorFromString(@"applicationIdentifier")];
                //应用的名称
                NSString *appName = [temp performSelector:NSSelectorFromString(@"localizedName")];
                //应用的类型是系统的应用还是第三方的应用
                NSString * type = [temp performSelector:NSSelectorFromString(@"applicationType")];
                //应用的版本
                NSString * shortVersionString = [temp performSelector:NSSelectorFromString(@"shortVersionString")];
            
                NSString * resourcesDirectoryURL = [temp performSelector:NSSelectorFromString(@"containerURL")];
            
                NSLog(@"类型=%@应用的BundleId=%@ ++++应用的名称=%@版本号=%@\n%@",type,appBundleId,appName,shortVersionString,resourcesDirectoryURL);
            
            }
        }  
    }
    
    //调用App  
    - (void)openApp {
        PrivateApi_LSApplicationWorkspace* workspace = [NSClassFromString(@"LSApplicationWorkspace") new];      
        [workspace openApplicationWithBundleID:@"com.xxx.xxx"];  
    }
    

    //获取所有App包名
    1.获取到手机里面所有的APP包名

    - (void)touss
    {
        Class lsawsc = objc_getClass("LSApplicationWorkspace");
        NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
        NSArray *Arr = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];
        for (NSString * tmp in Arr)
        {
            NSString * bundleid = [self getParseBundleIdString:tmp];
            NSLog(@"%@",bundleid);
        }
    }
    
    - (NSString *)getParseBundleIdString:(NSString *)description
    {
        NSString * ret = @"";
        NSString * target = [description description];
        // iOS8.0 "LSApplicationProxy: com.apple.videos",
        // iOS8.1 "<LSApplicationProxy: 0x898787998> com.apple.videos",
        // iOS9.0 "<LSApplicationProxy: 0x145efbb0> com.apple.PhotosViewService <file:///Applications/PhotosViewService.app>"
        if (target == nil) {
            return ret;
        }
        NSArray * arrObj = [target componentsSeparatedByString:@" "];
        switch ([arrObj count]) {
            case 2: // [iOS7.0 ~ iOS8.1)
            case 3: {
                 // [iOS8.1 ~ iOS9.0)
                ret = [arrObj lastObject];
            }
              break;
            case 4: {
                // [iOS9 +) 
                ret = [arrObj objectAtIndex:2];
            }
              break;
            default:
                break;
        }
        return ret;
    }
    

    2.通过包名去打开应用

        Class lsawsc = objc_getClass("LSApplicationWorkspace");
        NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
        // iOS6 没有defaultWorkspace
        if ([workspace respondsToSelector:NSSelectorFromString(@"openApplicationWithBundleID:")])
        {
            [workspace performSelector:NSSelectorFromString(@"openApplicationWithBundleID:") withObject:@"com.Calendar.jbp"];
        }
    
    更新 来源()

    在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以上系统可正确运行。

    慢慢来,一步一个巴掌印~~~

    相关文章

      网友评论

          本文标题:iOS 获取手机安装的所有的Apps

          本文链接:https://www.haomeiwen.com/subject/kdladxtx.html