MobileCoreService這個系統的庫,裡面有個私有的類LSApplicationWorkspace
,利用運行時可以獲得私有類裡面的方法,-(id)allInstalledApplications; 該方法能夠獲得設備上所有的應用信息,包括系統的和用戶的應用
獲得的應用的信息是一個類對象LSApplicationProxy,該對象裡面有方法獲得app的版本,名稱,bundleID,類型
好了直接上代碼首先引入頭文件#include <objc/runtime.h>
- (void)getAllApps
{
//獲取手機上所有的app
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
NSArray *apps = [workspace performSelector:@selector(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")];
NSLog(@"類型=%@應用的BundleId=%@ ++++應用的名稱=%@版本號=%@",type,appBundleId,appName,shortVersionString);
}
}
}
這裡附上MobileCoreServices裡面的頭文件https://github.com/nst/iOS-Runtime-Headers/tree/master/Frameworks/MobileCoreServices.framework