Created
February 27, 2015 05:20
-
-
Save msching/3091a2b3396ff4fff771 to your computer and use it in GitHub Desktop.
A UIDevice category for getting the running processes on an iOS device. Based on something floating about on the web by Landon Fuller. Cleaned it up a bit and changed so it returns an NSArray of NSDictionary[s].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// .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"]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment