如何在 App Store 批准的应用程序中获取有关可用内存和运行进程的信息?(是的,有一个!) [英] How to get information about free memory and running processes in an App Store approved app? (Yes, there is one!)
问题描述
App Store 中有一个叫做Activity Monitor Touch"的应用,可以显示后台进程和空闲内存.
There is an app called "Activity Monitor Touch" in the App Store, which displays background processes as well as free memory.
所以必须有一个公共 API 来访问这些信息.证据:
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.?
直到最近,我才确信 iOS 上绝对不可能出现这样的事情.
Until recently I was sure that something like this is absolutely impossible on 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(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;
}
但我无法让它在 iPhone 上运行.Xcode 不知道这些符号:CTL_KERN、KERN_PROC、KERN_PROC_ALL
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?
推荐答案
像魅力一样工作:
#import <sys/sysctl.h>
这篇关于如何在 App Store 批准的应用程序中获取有关可用内存和运行进程的信息?(是的,有一个!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!