我能否编程方式找出我iOS设备上安装的所有应用程序名称?是否有相应的API可用?
感谢您的帮助。
不,由于沙盒环境的限制,iOS应用程序无法访问其他应用程序的信息。
-(void) allInstalledApp
{
NSDictionary *cacheDict;
NSDictionary *user;
static NSString *const cacheFileName = @"com.apple.mobile.installation.plist";
NSString *relativeCachePath = [[@"Library" stringByAppendingPathComponent: @"Caches"] stringByAppendingPathComponent: cacheFileName];
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent: @"../.."] stringByAppendingPathComponent: relativeCachePath];
cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];
user = [cacheDict objectForKey: @"User"];
NSDictionary *systemApp=[cacheDict objectForKey:@"System"];
}
systemApp
字典
包含所有系统相关应用程序的列表,而 user
字典
则包含其他应用程序信息。
并非从设备上进行操作,但是你可以从桌面上查看iTunes库。
尝试这个,即使是非越狱设备也可以使用:
#include <objc/runtime.h>
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
SEL selector=NSSelectorFromString(@"defaultWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
SEL selectorALL = NSSelectorFromString(@"allApplications");
NSLog(@"apps: %@", [workspace performSelector:selectorALL]);//will give you all **Bundle IDS** of user's all installed apps
LSApplicationProxy
对象数组。Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:NSSelectorFromString(@"defaultWorkspace")];
NSMutableArray *array = [workspace performSelector:NSSelectorFromString(@"allApplications")];
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
for (id lsApplicationProxy in array) {
if(nil != [lsApplicationProxy performSelector:NSSelectorFromString(@"itemName")]){
[mutableArray addObject:[lsApplicationProxy performSelector:NSSelectorFromString(@"itemName")]];
}
}
NSLog(@"********* Applications List ************* : \n %@",mutableArray);
<objc/runtime.h>
。canOpenURL
方法来检查应用程序是否已安装,或者通过检查后台进程并将其与你感兴趣的应用程序名称进行匹配来实现。