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

来源:互联网 发布:怎么登录网站数据库 编辑:程序博客网 时间:2024/05/22 13:49

           主要思路就是 ,在要被识别的应用程序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。

2.png 

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


3.png 




        2. 然后 在主动设备的应用程序A中,通过   这个方法判断手机中是否存在这个应用B
         [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"XXX://"]];
         如果返回YES则表示此应用在手机中安装过,反之则没有安装过.


         具体代码如下:

         

[cpp] view plaincopy
  1. -(BOOL)  APCheckIfAppInstalled2:(NSString *)urlSchemes  
  2. {  
  3.     if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlSchemes]])  
  4.     {  
  5.         NSLog(@" installed");  
  6.   
  7.         return  YES;  
  8.     }  
  9.     else  
  10.     {  
  11.         return  NO;  
  12.     }  
  13. }  

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


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


   还有另外一个 查看 

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


   代码如下:

   

[cpp] view plaincopy
  1. -(BOOL)  APCheckIfAppInstalled:(NSString *)bundleIdentifier  
  2. {  
  3.     static NSString *const cacheFileName = @"com.apple.mobile.installation.plist";  
  4.     NSString *relativeCachePath = [[@"Library" stringByAppendingPathComponent: @"Caches"] stringByAppendingPathComponent: cacheFileName];  
  5.       
  6.     NSDictionary *cacheDict = nil;  
  7.     NSString *path = nil;  
  8.     // Loop through all possible paths the cache could be in  
  9.     for (short i = 0; 1; i++)  
  10.     {  
  11.         switch (i)  
  12.         {  
  13.             case 0: // Jailbroken apps will find the cache here; their home directory is /var/mobile  
  14.                 path = [NSHomeDirectory() stringByAppendingPathComponent: relativeCachePath];  
  15.                 break;  
  16.             case 1: // App Store apps and Simulator will find the cache here; home (/var/mobile/) is 2 directories above sandbox folder  
  17.                 path = [[NSHomeDirectory() stringByAppendingPathComponent: @"../.."] stringByAppendingPathComponent: relativeCachePath];  
  18.                 break;  
  19.             case 2: // If the app is anywhere else, default to hardcoded /var/mobile/  
  20.                 path = [@"/var/mobile"      stringByAppendingPathComponent: relativeCachePath];  
  21.                 break;  
  22.       
  23.             default// Cache not found (loop not broken)  
  24.                 return NO;  
  25.             break;  
  26.         }  
  27.           
  28.         BOOL isDir = NO;  
  29.         if ([[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] ) // Ensure that file exists  
  30.         {  
  31.             if (isDir == YES)  
  32.             {  
  33.                 NSLog(@"Dir");  
  34.             }  
  35.             else  
  36.             {  
  37.                 cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];  
  38.             }  
  39.         }  
  40.       
  41.           
  42.         if (cacheDict) // If cache is loaded, then break the loop. If the loop is not "broken," it will return NO later (default: case)  
  43.             break;  
  44.     }  
  45.       
  46.     NSDictionary *system = [cacheDict objectForKey: @"System"]; // First check all system (jailbroken) apps  
  47.     if ([system objectForKey: bundleIdentifier])  
  48.         return YES;  
  49.       
  50.     NSDictionary *user = [cacheDict objectForKey: @"User"]; // Then all the user (App Store /var/mobile/Applications) apps  
  51.       
  52.     if ([user objectForKey: bundleIdentifier])  
  53.         return YES;  
  54.       
  55.     // If nothing returned YES already, we'll return NO now  
  56.     return NO;  
  57. }  
0 0
原创粉丝点击