您的位置:首页 > 移动开发 > IOS开发

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

2014-11-11 16:49 435 查看
主要思路就是 ,在要被识别的应用程序B的XCode的info.plist中

如果是Xcode 4.2 ,那么

1. 在info.plist 中 增加 一个 URL Schemes: XXX

添加的具体细节是:

1.1 打开 info.plist ,在 Information Property List的下面增加一项:URL types

1.2 然后在 URL Types 下面增加一项 Item 0 ,这是个Dictionary

1.3 在 Item0 下面增加一个 URL Schemes 类型的 Array

1.4 然后在URL Schemes 的下面增加一个 URL identifier ,String值可以不填

在 Item0 的下面增加一个 Item0, String值就是 XXX 就可以了。

开始在网上 看到说添加 URL Schemes ,以为直接添加到 Information Property List 下面的一项,key为 URL Schemes ,值为 XXX就ok了,后面发现不起作用。这个里面只能添加指定的类型,会自动提示相关的类型,正确的方法是上面的过程。

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

解决方案:

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



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



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

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

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

具体代码如下:

[cpp] view
plaincopy

-(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 来判断,但是只能判断越狱机,因为越狱机才能访问到这个文件,在非越狱的机器中,因为不允许应用程序访问沙盒环境以外的目录,所以不能读取这个文件,甚至判断这个文件是否存在都会失败。

代码如下:

[cpp] view
plaincopy

-(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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: