zoukankan      html  css  js  c++  java
  • 获取IOS应用安装列表

    原文转载至 http://blog.csdn.net/justinjing0612/article/details/8887747

    转自鸟哥博客:http://blog.cnrainbird.com/index.php/2012/04/19/guan_yu_huo_qu_ios_ying_yong_lie_biao/?utm_source=rss

    当两天没事儿,突然想起这么一个命题:获取IOS应用安装列表。

    研究来研究去最后也没有得出个所以然来。这不今天上网,发现这篇儿文章。晾这说有三种方法。也就顺便总结一下,边转载边补充。

    ok,说是三种方法,靠谱的两种:

    1.openURL

    我们知道可以给应用设置URL Scheme,这样别的应用就可以通过这个地址打开咱们的应用。其实还有一个api叫canOpenURL.这样如果咱们知道要检查的IOS应用列表的URL Scheme的话,就可以用canOpenURL检查一下。

    2.获取运行程序列表

    1. </pre><pre name="code" class="html">// .h  
    2.   
    3. @interface UIDevice (ProcessesAdditions)  
    4. - (NSArray *)runningProcesses;  
    5. @end  
    6.   
    7. // .m  
    8. #import <sys/sysctl.h>  
    9.   
    10. @implementation UIDevice (ProcessesAdditions)  
    11.   
    12. - (NSArray *)runningProcesses {  
    13.   
    14.         int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};  
    15.         size_t miblen = 4;  
    16.           
    17.         size_t size;  
    18.         int st = sysctl(mib, miblen, NULL, &size, NULL, 0);  
    19.           
    20.         struct kinfo_proc * process = NULL;  
    21.         struct kinfo_proc * newprocess = NULL;  
    22.           
    23.     do {  
    24.                   
    25.         size += size / 10;  
    26.         newprocess = realloc(process, size);  
    27.                   
    28.         if (!newprocess){  
    29.                           
    30.             if (process){  
    31.                 free(process);  
    32.             }  
    33.                           
    34.             return nil;  
    35.         }  
    36.                   
    37.         process = newprocess;  
    38.         st = sysctl(mib, miblen, process, &size, NULL, 0);  
    39.                   
    40.     } while (st == -1 && errno == ENOMEM);  
    41.           
    42.         if (st == 0){  
    43.                   
    44.                 if (size % sizeof(struct kinfo_proc) == 0){  
    45.                         int nprocess = size / sizeof(struct kinfo_proc);  
    46.                   
    47.                         if (nprocess){  
    48.                           
    49.                                 NSMutableArray * array = [[NSMutableArray alloc] init];  
    50.           
    51.                                 for (int i = nprocess - 1; i >= 0; i--){  
    52.                   
    53.                                         NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];  
    54.                                         NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];  
    55.                   
    56.                                         NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]   
    57.                                                                                                                                                 forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];  
    58.                                         [processID release];  
    59.                                         [processName release];  
    60.                                         [array addObject:dict];  
    61.                                         [dict release];  
    62.                                 }  
    63.           
    64.                                 free(process);  
    65.                                 return [array autorelease];  
    66.                         }  
    67.                 }  
    68.         }  
    69.           
    70.         return nil;  
    71. }  
    72.   
    73. @end  
    74.   
    75. // Example usage.  
    76. NSArray * processes = [[UIDevice currentDevice] runningProcesses];  
    77. for (NSDictionary * dict in processes){  
    78.         NSLog(@"%@ - %@", [dict objectForKey:@"ProcessID"], [dict objectForKey:@"ProcessName"]);  

    这种方法是获取运行中的应用列表。如果应用没被运行过或不在后台,就得不到喽。

    比起上面两个方法要靠谱一点儿的就是私有API了。

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


    不过这种方法需要机器已经越狱,还需要你的应用不在沙盒里,由于后一条笔者还不大会搞,所以没试成功:)

  • 相关阅读:
    Vue 中样式穿透 /deep/
    Vue 数据冻结 Object.freeze
    Vue 启动项目内存溢出
    Typora[ markdown ] 使用3之----- 语法高亮显示
    Typora[ markdown ] 使用2之-----空格显示
    手动创建mysql数据库的语句记录
    api不能自动注入条件的解决方法
    【WTM框架】查询列表显示正常,但是导出的时候查询条件不起作用的问题记录及解决方法
    WTM问题之“数据列表”控件出现横向的滚动条的解决方法
    树莓派docker无法限制内存Your kernel does not support memory limit capabilities or the cgroup is not mounted
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4054189.html
Copyright © 2011-2022 走看看