美文网首页
iOS 获取是否安装了某个App

iOS 获取是否安装了某个App

作者: rockyMJ | 来源:发表于2018-05-30 16:25 被阅读55次

    *必要条件:需要知道这个App的bundleId

    - (BOOL)isHadCompleteWithBundleId:(NSString *)bundleId {
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) {
            NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
            if ([container load]) {
                Class appContainer = NSClassFromString(@"MCMAppContainer");
                
                id test = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:_listModel.bundleId withObject:nil];
                NSLog(@"%@",test);
                if (test) {
                    return YES;
                } else {
                    return NO;
                }
            }
            return NO;
            
        }else{
            //低于iOS 11
    //方法的实现见下面
            NSArray *arr = [[XBInfoConfiger shareInfo] touss];
            if ([arr containsObject:_listModel.bundleId]) {
                return YES;
            }else{
                return NO;
            }
        }
    }
    
    //1.获取到手机里面所有的APP包名
    
    - (NSArray*)touss
    {
        Class lsawsc = objc_getClass("LSApplicationWorkspace");
        NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
        NSArray *Arr = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];
        NSArray *arr = [workspace performSelector:NSSelectorFromString(@"allApplications")];
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSString * tmp in Arr)
        {
            NSString * bundleid = [self getParseBundleIdString:tmp];
            if (bundleid.length > 0) {
                [tempArr addObject:bundleid];
            }
    //        NSLog(@"-----%@------",bundleid);
        }
        return [tempArr copy];
    }
    
    - (NSString *)getParseBundleIdString:(NSString *)description
    {
        NSString * ret = @"";
        NSString * target = [description description];
        
      
        
        if (target == nil)
        {
            return ret;
        }
        NSArray * arrObj = [target componentsSeparatedByString:@" "];
        if (SYS_Version >= 9.0) {
            ret = [arrObj objectAtIndex:2];
        }else{
            ret = [arrObj lastObject];
        }
        
        return ret;
    }
    

    相关文章

      网友评论

          本文标题:iOS 获取是否安装了某个App

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