44

There is an app called "Activity Monitor Touch" in the App Store, which displays background processes as well as free memory.

So there MUST be an public API to access this information. The evidence:

evidence

evidence

I'm already searching for days but can't find any good starting point. How can this app figure all this stuff out without any jailbreaking / hacking / etc.?

Until recently I was sure that something like this is absolutely impossible on iOS.

I've found this code snippet:

- (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;
}

But I can't make it run on the iPhone. Xcode doesn't know these symbols: CTL_KERN, KERN_PROC, KERN_PROC_ALL

So of course I must import a header file or library. Does anyone know where these belong to, and how the headers must be imported to make this work?

Community
  • 1
  • 1
dontWatchMyProfile
  • 42,456
  • 49
  • 169
  • 255
  • [potentially related question linked here](http://stackoverflow.com/questions/3823266/is-it-legal-to-use-the-well-known-free-memory-code-in-ipad-iphone-app) – Michael Dautermann Nov 26 '11 at 01:24
  • But is it officially allowed by Apple to use it?! Maybe the Apps in Store just had luck to get into?! – davidOhara Jan 29 '14 at 11:33
  • This app is no longer available. But using sysctl is not banned from apps. See a bunch of apps that do similar stuff. Search for activity monitor on iOS store. – Dickey Singh Feb 05 '14 at 22:51
  • There's an app currently in the Appstore that lists all the running apps and even shows the app icons. The app can also "free up" unused memory and "clean up" disk cache. See app Battery Doctor. – Jay Q. Aug 21 '14 at 00:58
  • @dontWatchMyProfile So you actually can know the name of the app running with this? – huggie Jan 12 '15 at 11:18
  • 1
    how do they get applications icons? – BergP Mar 19 '15 at 17:37

2 Answers2

35

Works like a charm:

#import <sys/sysctl.h>
mackworth
  • 5,655
  • 2
  • 24
  • 49
31

sysctl is no longer accessible to sandboxed iOS 9 apps.

From WWDC 2015 session 703 Privacy and Your App:

In iOS 9, the sandbox now prevents a process from accessing the kern.proc, kern.procargs, and kern.procargs2 values for other processes

and

iOS apps are not permitted to see what other apps are running

So even if you find a way, you are likely to get rejected from the App Store.

https://developer.apple.com/videos/play/wwdc2015-703/

Sam Corder
  • 5,151
  • 3
  • 21
  • 30
Rob MacEachern
  • 811
  • 8
  • 13
  • 1
    Thank you Ad Networks. This is why we can't have nice things. Yes, China, I'm looking in your general direction. – picciano Nov 16 '15 at 22:04