有的时候,我们需要在实现应用间调用功能,系统自带的方式固然方便。
但是,如果我们能在调用前先获取系统的应用列表岂不更爽快。因此,今天
我们就来实现获取应用列表的功能。直接上代码!
+ (BOOL) APCheckIfAppInstalled:(NSString *)bundleIdentifier
{
staticNSString *const cacheFileName =@"com.apple.mobile.installation.plist";
NSString *relativeCachePath = [[@"Library"stringByAppendingPathComponent:@"Caches"]stringByAppendingPathComponent: cacheFileName];
NSDictionary *cacheDict =nil;
NSString *path =nil;
// Loop through all possible paths the cache could be in
for (short i =0;1; i++)
{
switch (i) {
case0:// Jailbroken apps will find the cache here; their home directory is /var/mobile
path = [NSHomeDirectory()stringByAppendingPathComponent: relativeCachePath];
break;
case1:// App Store apps and Simulator will find the cache here; home (/var/mobile/) is 2 directories above sandbox folder
path = [[NSHomeDirectory()stringByAppendingPathComponent:@"../.."]stringByAppendingPathComponent: relativeCachePath];
break;
case2:// If the app is anywhere else, default to hardcoded /var/mobile/
path = [@"/var/mobile"stringByAppendingPathComponent: relativeCachePath];
break;
default:// Cache not found (loop not broken)
returnNO;
break; }
BOOL isDir =NO;
if ([[NSFileManagerdefaultManager]fileExistsAtPath: pathisDirectory: &isDir] && !isDir)// Ensure that file exists
cacheDict = [NSDictionarydictionaryWithContentsOfFile: path];
if (cacheDict)//If cache is loaded, then break the loop. If the loop is not "broken," it will return NO later (default: case)
break;
}
NSDictionary *system = [cacheDictobjectForKey:@"System"];// First check all system (jailbroken) apps
if ([systemobjectForKey: bundleIdentifier])returnYES;
NSDictionary *user = [cacheDictobjectForKey:@"User"];// Then all the user (App Store /var/mobile/Applications) apps
if ([userobjectForKey: bundleIdentifier])returnYES;
// If nothing returned YES already, we'll return NO now
return NO;
}