在iOS 11 以前我們可以使用LSApplicationWorkspace來(lái)獲取手機(jī)上已安裝的應(yīng)用列表
- (void)getIphoneAllApplications {
? ? ClassLSApplicationWorkspace_class =objc_getClass("LSApplicationWorkspace");
? ? NSObject* workspace = [LSApplicationWorkspace_classperformSelector:@selector(defaultWorkspace)];
? ? NSArray*apps= [workspaceperformSelector:@selector(allApplications)];
? ? ClassLSApplicationProxy_class =objc_getClass("LSApplicationProxy");
? ? for(inti =0; i < apps.count; i++) {
? ? ? ? NSObject*temp = apps[i];
? ? ? ? if([tempisKindOfClass:LSApplicationProxy_class])
? ? ? ? {
? ? ? ? ? ? //應(yīng)用的bundleId
? ? ? ? ? ? NSString*appBundleId = [tempperformSelector:NSSelectorFromString(@"applicationIdentifier")];
? ? ? ? ? ? //應(yīng)用的名稱
? ? ? ? ? ? NSString *appName = [temp performSelector:NSSelectorFromString(@"localizedName")];
? ? ? ? ? ? //應(yīng)用的類(lèi)型是系統(tǒng)的應(yīng)用還是第三方的應(yīng)用
? ? ? ? ? ? NSString * type = [temp performSelector:NSSelectorFromString(@"applicationType")];
? ? ? ? ? ? //應(yīng)用的版本
? ? ? ? ? ? NSString* shortVersionString = [tempperformSelector:NSSelectorFromString(@"shortVersionString")];
? ? ? ? ? ? NSString* resourcesDirectoryURL = [tempperformSelector:NSSelectorFromString(@"containerURL")];
? ? ? ? ? ? NSLog(@"類(lèi)型=%@應(yīng)用的BundleId=%@ ++++應(yīng)用的名稱=%@版本號(hào)=%@\n%@",type,appBundleId,appName,shortVersionString,resourcesDirectoryURL);
? ? ? ? }
? ? }
}
iOS 11 上獲取所有已安裝應(yīng)用接口被禁,但可以根據(jù)BundleId檢查App是否存在
- (BOOL)isInstalled:(NSString *)bundleId {
? ? NSBundle *container = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/MobileContainerManager.framework"];
? ? if ([container load]) {
? ? ? ? Class appContainer = NSClassFromString(@"MCMAppContainer");#pragmaclang diagnostic push#pragmaclang diagnostic ignored "-Wundeclared-selector"idcontainer = [appContainer performSelector:@selector(containerWithIdentifier:error:) withObject:bundleId withObject:nil];#pragmaclang diagnostic pop? ? ? ? NSLog(@"%@", [container performSelector:@selector(identifier)]);
? ? ? ? if (container) {
? ? ? ? ? ? return YES;
? ? ? ? } else {
? ? ? ? ? ? return NO;
? ? ? ? }
? ? }
? ? return NO;
}