13

Can we retrieve the applications currently running in iPhone and iPad?

UPDATE

Can we do it in jail broken phones? Can we do it for an app for CYDIA Store?

RK-
  • 12,099
  • 23
  • 89
  • 155

2 Answers2

29

You can get a list of running processes and from process ids may be you can figure out which ones are system processes and which one are 3rd party apps, but anyway I don't believe you can use it in application for appstore. (code taken from here)

- (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;
}:
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • 2
    @Krishnan, this code works on non-jailbroken device, but Apple may forbid using this feature in application. If you target Cydia store you can do that ( I think (but not 100% sure) I've seen an application that works like a task manager - displaying all running application and allowing to terminate them) – Vladimir Nov 30 '10 at 11:41
  • Thanks for that. If you get to know any other info regarding this, please share it. – RK- Nov 30 '10 at 11:43
  • 5
    You can definitely do something like this on the App Store. Here's the evidence: http://stackoverflow.com/questions/8275578/how-to-get-information-about-free-memory-and-running-processes – dontWatchMyProfile Nov 26 '11 at 01:23
  • @DmitryIsakov have you found a way around this for iOS9+ ? – Valerio Santinelli Oct 27 '16 at 16:02
  • @ValerioSantinelli: This method works *if you are outside of the sandbox*. If you want the list, create an unsandboxed helper and then use IPC to fetch it from that helper. – kennytm Oct 31 '16 at 18:33
2

Why would you loop until you run put of memory? I think this is a lot simpler ;)

size_t size;
struct kinfo_proc *procs = NULL;
int status;

int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };

status  = sysctl(mib, 4, NULL, &size, NULL, 0);
procs   = malloc(size);
status  = sysctl(mib, 4, procs, &size, NULL, 0);
hackerdiehack
  • 372
  • 3
  • 11