本文介绍了如何在App Store批准的应用程序中获取有关可用内存和运行进程的信息? (是的,有一个!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

App Store中有一个名为Activity Monitor Touch的应用程序,它显示后台进程和可用内存。



所以必须有一个公共API访问此信息。证据:









直到最近,我确信这样的东西在iOS上是绝对不可能的。



我此代码段:

   - (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(进程,大小);

if(!newprocess){

if(process){
free(process);
}

返回零;
}

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

免费(流程);
return [array autorelease];
}
}
}

返回零;
}

但我不能让它在iPhone上运行。 Xcode不知道这些符号:
CTL_KERN,KERN_PROC,KERN_PROC_ALL



所以当然我必须导入头文件或库。有谁知道这些属于哪里,以及如何导入标题以使其工作?

解决方案

像魅力一样工作:

  #import< sys / sysctl.h> 


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:

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?

解决方案

Works like a charm:

#import <sys/sysctl.h>

这篇关于如何在App Store批准的应用程序中获取有关可用内存和运行进程的信息? (是的,有一个!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 11:48
查看更多