获取IOS应用列表
2013-06-13 20:19
399 查看
获取运行程序列表
// .h
这种方法是获取运行中的应用列表。如果应用没被运行过或不在后台,就得不到喽。
比起上面两个方法要靠谱一点儿的就是私有API了。
不过这种方法需要机器已经越狱,还需要你的应用不在沙盒里,由于后一条笔者还不大会搞,所以没试成功:)
// .h
@interface UIDevice (ProcessesAdditions) - (NSArray *)runningProcesses; @end // .m #import <sys/sysctl.h> @implementation UIDevice (ProcessesAdditions) - (NSArray *)runningProcesses { int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; size_t miblen = 4; size_t size; int st = sysctl(mib, miblen, NULL, &size, NULL, 0); struct kinfo_proc * process = NULL; struct kinfo_proc * newprocess = NULL; do { size += size / 10; newprocess = realloc(process, size); if (!newprocess){ if (process){ free(process); } return nil; } process = newprocess; st = sysctl(mib, miblen, process, &size, NULL, 0); } while (st == -1 && errno == ENOMEM); if (st == 0){ if (size % sizeof(struct kinfo_proc) == 0){ int nprocess = size / sizeof(struct kinfo_proc); if (nprocess){ NSMutableArray * array = [[NSMutableArray alloc] init]; for (int i = nprocess - 1; i >= 0; i--){ NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid]; NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm]; NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil] forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]]; [processID release]; [processName release]; [array addObject:dict]; [dict release]; } free(process); return [array autorelease]; } } } return nil; } @end // Example usage. NSArray * processes = [[UIDevice currentDevice] runningProcesses]; for (NSDictionary * dict in processes){ NSLog(@"%@ - %@", [dict objectForKey:@"ProcessID"], [dict objectForKey:@"ProcessName"]); }
这种方法是获取运行中的应用列表。如果应用没被运行过或不在后台,就得不到喽。
比起上面两个方法要靠谱一点儿的就是私有API了。
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; NSLog(@"relativeCachePath:%@",relativeCachePath); // 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; NSLog(@"path:%@",path); // Ensure that file exists if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] && !isDir){ cacheDict = [NSDictionary dictionaryWithContentsOfFile: path]; } // If cache is loaded, then break the loop. If the loop is not "broken," it will return NO later (default: case) if (cacheDict){ NSLog(@"cacheDict:%@",cacheDict); break; } } NSLog(@"gggg"); // First check all system (jailbroken) apps NSDictionary *system = [cacheDict objectForKey: @"System"]; NSLog(@"system:%@",system); if ([system objectForKey: bundleIdentifier]){ return YES; } // Then all the user (App Store /var/mobile/Applications) apps NSDictionary *user = [cacheDict objectForKey: @"User"]; NSLog(@"user:%@",user); if ([user objectForKey: bundleIdentifier]){ return YES; } // If nothing returned YES already, we'll return NO now return NO; }
不过这种方法需要机器已经越狱,还需要你的应用不在沙盒里,由于后一条笔者还不大会搞,所以没试成功:)
相关文章推荐
- 获取IOS应用安装列表
- ios开发 如何在应用内获取当前周围wifi列表和强度 并实现在应用内控制wifi开关
- ios获取已安装的应用列表
- 获取IOS应用安装列表
- 使用iOS私有API获取安装应用列表
- iOS 11适配:LSApplicationWorkspace无法获取应用安装列表解决方案
- iOS获取已安装应用列表
- 关于获取IOS应用列表
- 获取IOS应用安装列表
- 获取IOS应用安装列表
- ios开发不越狱获取本地应用列表通过调用运行时库
- iOS获取应用列表
- 获取IOS应用安装列表
- iOS获取已安装的app列表(私有库)+ 通过包名打开应用
- iOS判断应用是否获取到系统相机 相册的授权 以及如何请求授权
- 【iOS开放】应用从App Store获取版本信息
- [iOS 获取AppStored 中应用的下载地址]
- iOS 获取 当前设备 可用内存 及当前 应用 所占内存
- iOS应用内付费(IAP)开发步骤列表
- iOS 利用 NEHotspotHelper 获取 WiFi 列表