原文转载至 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. @interface UIDevice (ProcessesAdditions)
  3. - (NSArray *)runningProcesses;
  4. @end
  5. // .m
  6. #import <sys/sysctl.h>
  7. @implementation UIDevice (ProcessesAdditions)
  8. - (NSArray *)runningProcesses {
  9. int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
  10. size_t miblen = 4;
  11. size_t size;
  12. int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
  13. struct kinfo_proc * process = NULL;
  14. struct kinfo_proc * newprocess = NULL;
  15. do {
  16. size += size / 10;
  17. newprocess = realloc(process, size);
  18. if (!newprocess){
  19. if (process){
  20. free(process);
  21. }
  22. return nil;
  23. }
  24. process = newprocess;
  25. st = sysctl(mib, miblen, process, &size, NULL, 0);
  26. } while (st == -1 && errno == ENOMEM);
  27. if (st == 0){
  28. if (size % sizeof(struct kinfo_proc) == 0){
  29. int nprocess = size / sizeof(struct kinfo_proc);
  30. if (nprocess){
  31. NSMutableArray * array = [[NSMutableArray alloc] init];
  32. for (int i = nprocess - 1; i >= 0; i--){
  33. NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
  34. NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
  35. NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]
  36. forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];
  37. [processID release];
  38. [processName release];
  39. [array addObject:dict];
  40. [dict release];
  41. }
  42. free(process);
  43. return [array autorelease];
  44. }
  45. }
  46. }
  47. return nil;
  48. }
  49. @end
  50. // Example usage.
  51. NSArray * processes = [[UIDevice currentDevice] runningProcesses];
  52. for (NSDictionary * dict in processes){
  53. 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. // If cache is loaded, then break the loop. If the loop is not "broken," it will return NO later (default: case)
  30. if (cacheDict){
  31. NSLog(@"cacheDict:%@",cacheDict);
  32. break;
  33. }
  34. }
  35. NSLog(@"gggg");
  36. // First check all system (jailbroken) apps
  37. NSDictionary *system = [cacheDict objectForKey: @"System"];
  38. NSLog(@"system:%@",system);
  39. if ([system objectForKey: bundleIdentifier]){
  40. return YES;
  41. }
  42. // Then all the user (App Store /var/mobile/Applications) apps
  43. NSDictionary *user = [cacheDict objectForKey: @"User"];
  44. NSLog(@"user:%@",user);
  45. if ([user objectForKey: bundleIdentifier]){
  46. return YES;
  47. }
  48. // If nothing returned YES already, we'll return NO now
  49. return NO;
  50. }

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

获取IOS应用安装列表的更多相关文章

  1. iOS学习——获取iOS设备的各种信息

    不管是在Android开发还是iOS开发过程中,有时候我们需要经常根据设备的一些状态或信息进行不同的设置和性能配置,例如横竖屏切换时,电池电量低时,内存不够时,网络切换时等等,我们在这时候需要进行一些 ...

  2. iOS11 获取手机已安装应用列表

    在iOS 11 以前我们可以使用LSApplicationWorkspace来获取手机上已安装的应用列表 iOS 11 上获取所有已安装应用接口被禁,但可以根据BundleId检查App是否存在 - ...

  3. python获取软件安装列表2222

    softer_installed_list ===================== 使用python编写的,获取本机软件安装列表,输出为html表格. * win7 32位环境下运行 * 使用的是 ...

  4. 利用RxJava获取手机已安装的App的图片、应用名称和版本号

    先上效果图: 获取手机已安装的App列表利用Android系统API就可以办到,这里为什么要引入RxJava?现在我们假设一下有下面几个需求: 1.我们不需要所有的App,只需要用户安装的第三方App ...

  5. 怎么获取iOS的私有API

    前言 作为iOS开发人员,相信大多数伙伴都对怎么获取iOS的私有API很有兴趣,最近通过查找资料,总结了以下三种方法,希望对你有用. 第一种(class-dump) 不得不说这是一个很棒的工具,安装和 ...

  6. iOS TableView多级列表

    代码地址如下:http://www.demodashi.com/demo/15006.html 效果预览 ### 一.需求 TableView多级列表:分级展开或合并,逐级获取并展示其子级数据,可以设 ...

  7. iOS系统声音列表

    iOS系统声音列表 效果 说明 1. 点击cell就能发出声音 2. 只需要给出声音编号,就可以,非常简单易用 源码 https://github.com/YouXianMing/iOS-Utilit ...

  8. 获取ios设备的udid

    今天get的第二个技能~~~ UDID指的是设备的唯一设备识别符,ipa包未上架之前如果不添加udid是无法安装成功的.那么如何快速获取ios设备的udid呢? 今天get的方法是用蒲公英,网址:ht ...

  9. Unity3D开发之“获取IOS设备所在的国家代码"

    原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/ 在前一段时间游戏开发中需要实现获取IOS设备所在的国家代 ...

随机推荐

  1. Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) 继续跪一把

    这次的前三题挺简单的,可是我做的不快也不对. A. Bank Robbery time limit per test 2 seconds memory limit per test 256 megab ...

  2. 【UML】概述以及面向对象技术总结

    导读:结束了软工文档后,就开始了UML的学习,不管学习什么,都要先从整体上去把握,然后再从细节上去分析理解.在视频的开头,就对UML进行了概述.然后接着讲了面向对象技术,用例图,类图和包图等.看着软工 ...

  3. hdu 4251 The Famous ICPC Team Again划分树入门题

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. 九度oj 题目1252:回文子串

    题目描述: 输入一个字符串,输出该字符串中对称的子字符串的最大长度. 比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. 输入: 存在多组数据,每组数据一行字 ...

  5. 洛谷P1435 回文字串

    题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...

  6. 【扩展kmp+最小循环节】HDU 4333 Revolving Digits

    http://acm.hdu.edu.cn/showproblem.php?pid=4333 [题意] 给定一个数字<=10^100000,每次将该数的第一位放到放到最后一位,求所有组成的不同的 ...

  7. 【NOI Linux】复习一波命令行

    $linux$ 终端真是用不惯. 假设 a 是一个可执行文件( $linux$ 下的可执行文件没有后缀 ) 1. size a 计算一个程序的静态内存(全局数组变量.栈空间.堆空间等),单位是字节.除 ...

  8. 【SPOJ220】Relevant Phrases of Annihilation(后缀数组,二分)

    题意: n<=10,len<=1e4 思路: #include<cstdio> #include<cstring> #include<string> # ...

  9. 【HDOJ6354】Everything Has Changed(计算几何)

    题意: 给定一个平面和一个(0,0)为中心的大圆,有n个小圆保证没有两两之间相交与覆盖整个大圆的情况,求小圆覆盖后大圆的周长并 1≤m≤100, -1e3<=x[i],y[i]<=1e3, ...

  10. AtCoder Regular Contest 074F - Lotus Leaves

    $n \leq 300,m \leq 300$,$n*m$的格子里有起点有终点有空地有障碍,人会从起点选一个同行或同列空地跳过去,然后一直这样跳到终点.求至少删掉多少格子使得人跳不到终点. 首先S和T ...