獲取手機(jī)上所有的app信息
#pragma mark ––––––––––––– 獲取手機(jī)上所有的應(yīng)用
/// iOS12+
- (void)getAppInfoList {
Class LSApplicationWorkspace = objc_getClass("LSApplicationWorkspace");
Class LSApplicationProxy = objc_getClass("LSApplicationProxy");
id defaultWorkspace = [LSApplicationWorkspace performSelector:@selector(defaultWorkspace)];
// 此方法在iOS12+獲取不到
// id allApplications = [defaultWorkspace performSelector:@selector(allInstalledApplications)];
NSArray *plugins = [defaultWorkspace performSelector:@selector(installedPlugins)];
NSMutableSet *list = [[NSMutableSet alloc] init];
for (id plugin in plugins) {
id bundle = [plugin performSelector:@selector(containingBundle)];
if (bundle) {
[list addObject:bundle];
}
}
// 遍歷所有app信息
for (id plugin in list) {
// BundleID
NSString *bundleIdentifier = [plugin performSelector:@selector(bundleIdentifier)];
if (![bundleIdentifier containsString:@"com.apple"]) {
NSLog(@"bundleIdentifier -> %@", bundleIdentifier);
NSString *applicationDSID = [plugin performSelector:@selector(applicationDSID)];
NSLog(@"applicationDSID -> %@", applicationDSID);
NSString *applicationIdentifier = [plugin performSelector:@selector(applicationIdentifier)];
NSLog(@"applicationIdentifier -> %@", applicationIdentifier);
NSString *applicationType = [plugin performSelector:@selector(applicationType)];
NSLog(@"applicationType -> %@", applicationType);
NSString *dynamicDiskUsage = [plugin performSelector:@selector(dynamicDiskUsage)];
NSLog(@"dynamicDiskUsage -> %@", dynamicDiskUsage);
NSString *itemID = [plugin performSelector:@selector(itemID)];
NSLog(@"itemID -> %@", itemID);
NSString *itemName = [plugin performSelector:@selector(itemName)];
NSLog(@"itemName -> %@", itemName);
NSString *minimumSystemVersion = [plugin performSelector:@selector(minimumSystemVersion)];
NSLog(@"minimumSystemVersion -> %@", minimumSystemVersion);
NSString *requiredDeviceCapabilities = [plugin performSelector:@selector(requiredDeviceCapabilities)];
NSLog(@"requiredDeviceCapabilities -> %@", requiredDeviceCapabilities);
NSString *sdkVersion = [plugin performSelector:@selector(sdkVersion)];
NSLog(@"sdkVersion -> %@", sdkVersion);
NSString *shortVersionString = [plugin performSelector:@selector(shortVersionString)];
NSLog(@"shortVersionString -> %@", shortVersionString);
NSString *sourceAppIdentifier = [plugin performSelector:@selector(sourceAppIdentifier)];
NSLog(@"sourceAppIdentifier -> %@", sourceAppIdentifier);
NSString *staticDiskUsage = [plugin performSelector:@selector(staticDiskUsage)];
NSLog(@"staticDiskUsage -> %@", staticDiskUsage);
NSString *teamID = [plugin performSelector:@selector(teamID)];
NSLog(@"teamID -> %@", teamID);
NSString *vendorName = [plugin performSelector:@selector(vendorName)];
NSLog(@"vendorName -> %@", vendorName);
}
}
}
/// iOS10 -12屯援,判斷是否安裝app
- (BOOL)isAppInstalled:(NSString *)bundleId {
NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
if ([container load]) {
Class appContainer = NSClassFromString(@"MCMAppContainer");
id install = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleId];
if (install) {
return YES;
}
}
return NO;
}
/// iOS10-
- (void)getAppInfoList10 {
Class LSApplicationWorkspace = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [LSApplicationWorkspace performSelector:@selector(defaultWorkspace)];
NSArray *allApplications = [workspace performSelector:@selector(allApplications)];
Class LSApplicationProxy = object_getClass(@"LSApplicationProxy");
for (LSApplicationProxy in allApplications) {
// 查看一些信息
NSString *bundleID = [LSApplicationProxy performSelector:@selector(applicationIdentifier)];
NSString *version = [LSApplicationProxy performSelector:@selector(bundleVersion)];
NSString *shortVersionString = [LSApplicationProxy performSelector:@selector(shortVersionString)];
NSLog(@"bundleID -> %@", bundleID);
}
}
根據(jù)bundleId打開app
// YES:可以打開 NO:不可以打開
BOOL isOpenApp = [self isOpenApp:@"com.xxx.demo"];
NSLog(@"111======%d",isOpenApp);
//根據(jù)bundleID打開app
/// 直接打開某個(gè)APP
- (BOOL)isOpenApp:(NSString*)bundleIdentifier {
Class LSApplicationWorkspace = objc_getClass("LSApplicationWorkspace");
NSObject *workspace = [LSApplicationWorkspace performSelector:@selector(defaultWorkspace)];
// 沒有安裝返回NO
BOOL isOpenApp = [workspace performSelector:@selector(openApplicationWithBundleID:) withObject:bundleIdentifier];
return isOpenApp;
}
參考文章:http://www.reibang.com/p/6b1fa5e5cd5b