检查 iphone 是否安装某 app

转载自:http://blog.csdn.net/zengconggen/article/details/7714514

        开发中遇到一个问题:公司多个产品时,希望在一个产品A中可以直接打开另外的某个产品B。如果用户尚未安装产品B, 则打开app store下载页面。


          对于这个问题:有3个技术点:

        1、产品B是否已安装的检测。

         2、应用内打开其他app的技术:参考:https://developer.apple.com/library/IOs/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW18

         3、从应用跳转至app下载页面的技术:参考:http://blog.csdn.net/zengconggen/article/details/6789420


         对于第2、3点,本文略过不讲。现提供第1技术点方案:(说明:技术有风险,有可能导致app被拒。不过app store有类似上线作品。看RP)

解决方案1:(经验证:该方案真机ios5无效,模拟器可以)

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

解决方案2:参考:http://blog.csdn.net/zengconggen/article/details/7714466

本方案有限制:只能获取到用户最近运行过的app。对于已安装但是不在最近有运行的app。不适用。


解决方案3:

  1. BOOL isExsit = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"rumtel://com.rumtel.AudioManager"]];  
  2. NSLog(@"App %@ installed", identifier);  
  3. if (isExsit)   
  4. {  
  5.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"rumtel://com.rumtel.AudioManager?a=1&b=2#sect"]];  
  6. }  
  7. else  
  8. {  
  9.     //打开app store下载页面  
  10. }  

方案3需要知道B的打开地址。可用。最终方案确定为3

附:目前公开的app相关资料:http://wiki.akosma.com/IPhone_URL_Schemes


原文地址:https://www.cnblogs.com/java20130723/p/3212128.html