获取手机中所有安装的app方法
-(void)getAppPlist
{
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
NSArray*apps = [workspace performSelector:@selector(allApplications)];
// NSArray*appsActivity = [workspace performSelector:@selector(applicationForUserActivityDomainName)];
NSMutableArray*appsIconArr = [NSMutableArray array];
NSMutableArray*appsNameArr = [NSMutableArray array];
NSLog(@"apps: %@",apps );
[apps enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL* stop) {
NSDictionary*boundIconsDictionary = [obj performSelector:@selector(boundIconsDictionary)];
NSString*iconPath = [NSString stringWithFormat:@"%@/%@.png", [[obj performSelector:@selector(resourcesDirectoryURL)]path], [[[boundIconsDictionary objectForKey:@"CFBundlePrimaryIcon"]objectForKey:@"CFBundleIconFiles"]lastObject]];
UIImage*image = [[UIImage alloc]initWithContentsOfFile:iconPath];
id name = [obj performSelector:@selector(localizedName)];
if(image)
{
[appsIconArr addObject:image];
[appsNameArr addObject: name];
}
// NSLog(@"iconPath = %@", iconPath);
NSLog(@"name = %@", name);
// 输出app的属性
NSLog(@"%@",[self properties_aps:obj]);
NSLog(@"_____________________________________________\n");
NSDictionary * appPropertices = [self properties_aps:obj];
}];
//通过applicationIdentifier id。判断是否安装某个APP
BOOL isInstall = [workspace performSelector:@selector(applicationIsInstalled:) withObject:@"com.tencent.xin"];
if (isInstall) {
//通过bundle id。打开一个APP
[workspace performSelector:@selector(openApplicationWithBundleID:) withObject:@"com.tencent.xin"];
}else{
NSLog(@"您还没安装");
}
}
//获取app的属性。
- (NSDictionary *)properties_aps:(id)objc
{
NSMutableDictionary *props = [NSMutableDictionary dictionary];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([objc class], &outCount);
for (i = 0; i<outCount; i++)
{
objc_property_t property = properties[i];
const char* char_f =property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
id propertyValue = [objc valueForKey:(NSString *)propertyName];
if (propertyValue) [props setObject:propertyValue forKey:propertyName];
}
free(properties);
return props;
}
通过定时器不断的调度 可以用下面的方法得到 所有正在下载的app
-(void)updateInstallList{
void *lib = dlopen("/System/Library/Frameworks/MobileCoreServices.framework/MobileCoreServices", RTLD_LAZY);
if (lib)
{
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
id AAURLConfiguration1 = [LSApplicationWorkspace defaultWorkspace];
if (AAURLConfiguration1)
{
id arrApp = [AAURLConfiguration1 allApplications];
for (int i=0; i<[arrApp count]; i++) {
LSApplicationProxy *LSApplicationProxy = [arrApp objectAtIndex:i];
NSString* bundleId =[LSApplicationProxy applicationIdentifier];
NSString* name = [LSApplicationProxy localizedName];
NSProgress *progress = (NSProgress *)[LSApplicationProxy installProgress];
InstallingModel *model = [self getInstallModel:bundleId];
//如果是正在下载状态
if (progress)
{
//已经检测到的
if (model) {
model.progress = [progress localizedDescription];
model.status = [NSString stringWithFormat:@"%@",[[progress userInfo] valueForKey:@"installState"]];
//第一次检测到的
}else{
InstallingModel *model = [[InstallingModel alloc] init];
model.appName = name;
model.bundleID = bundleId;
model.progress = [progress localizedDescription];
model.status = [NSString stringWithFormat:@"%@",[[progress userInfo] valueForKey:@"installState"]];
[_installedAry addObject:model];
}
}
}
}
if (lib) dlclose(lib);
}
}
通过bundle id。判断是否安装和打开app
//通过applicationIdentifier id。判断是否安装某个APP
BOOL isInstall = [workspace performSelector:@selector(applicationIsInstalled:) withObject:@"com.tencent.xin"];
if (isInstall) {
//通过bundle id。打开一个APP
[workspace performSelector:@selector(openApplicationWithBundleID:) withObject:@"com.tencent.xin"];
}else{
NSLog(@"您还没安装");
}