如何判断ios设备中是否安装了某款应用

如果是Xcode 4.6 ,那么按照下面的方法添加:

解决方案:

从91SDK3.2.5开始要求接入方需要设置一个URL Scheme,设置方法如下:选中工程中的Target,选中Info标签页,找到底下的URL Types,展开,点击加号,创建一个新的URL Scheme。

171300xkniiaxbbnwq9yzy.png.thumb.jpg

点击后,Identifier字段填入你的软件标识符,URL Schemes字段填入格式为:91-xxxxx,其中xxxx为你的软件标识符。Role字段可以设置为None,Icon字段可以不填。示例如下:

1713376wx166ne1r66aqw6.png

2. 然后 在主动设备的应用程序A中,通过  这个方法判断手机中是否存在这个应用B

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString"XXX://"]];

复制代码

如果返回YES则表示此应用在手机中安装过,反之则没有安装过.

具体代码如下:

-(BOOL) APCheckIfAppInstalled2:(NSString *)urlSchemes

{

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlSchemes]])

{

NSLog(@" installed");

return YES;

}

else

{

return NO;

}

}

调用  APCheckIfAppInstalled2:XXX  就可以判断 是否安装了应用程序B 了。

这个方法不管对越狱过的iOS设备还是没有越狱过的设备都生效。

还有另外一个 查看

com.apple.mobile.installation.plist  系统文件的方法,通过 bundle identifier 来判断,但是只能判断越狱机,因为越狱机才能访问到这个文件,在非越狱的机器中,因为不允许应用程序访问沙盒环境以外的目录,所以不能读取这个文件,甚至判断这个文件是否存在都会失败。

代码如下:

-(BOOL) APCheckIfAppInstalled:(NSString *)bundleIdentifier

{

static NSString *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)

{

case 0: // Jailbroken apps will find the cache here; their home directory is /var/mobile

path = [NSHomeDirectory() stringByAppendingPathComponent: relativeCachePath];

break;

case 1: // 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;

case 2: // If the app is anywhere else, default to hardcoded /var/mobile/

path = [@"/var/mobile" stringByAppendingPathComponent: relativeCachePath];

break;

default: // Cache not found (loop not broken)

return NO;

break;

}

BOOL isDir = NO;

if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] ) // Ensure that file exists

{

if (isDir == YES)

{

NSLog(@"Dir");

}

else

{

cacheDict = [NSDictionary dictionaryWithContentsOfFile: 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 = [cacheDict objectForKey: @"System"]; // First check all system (jailbroken) apps

if ([system objectForKey: bundleIdentifier])

return YES;

NSDictionary *user = [cacheDict objectForKey: @"User"]; // Then all the user (App Store /var/mobile/Applications) apps

if ([user objectForKey: bundleIdentifier])

return YES;

// If nothing returned YES already, we'll return NO now

return NO;

}