ios 如何在 App Store 批准的应用程序中获取有关可用内存和运行进程的信息?(是的,有一个!)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8275578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get information about free memory and running processes in an App Store approved app? (Yes, there is one!)
提问by dontWatchMyProfile
There is an app called "Activity Monitor Touch" in the App Store, which displays background processes as well as free memory.
App Store 中有一个叫做“Activity Monitor Touch”的应用,可以显示后台进程以及空闲内存。
So there MUST be an public API to access this information. The evidence:
所以必须有一个公共 API 来访问这些信息。证据:
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.
直到最近我才确信在 iOS 上这样的事情是绝对不可能的。
I've foundthis 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
但我不能让它在 iPhone 上运行。Xcode 不知道这些符号: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?
所以当然我必须导入一个头文件或库。有谁知道这些属于哪里,以及必须如何导入标题才能使其工作?
采纳答案by mackworth
Works like a charm:
奇迹般有效:
#import <sys/sysctl.h>
回答by Rob MacEachern
sysctl
is no longer accessible to sandboxed iOS 9 apps.
sysctl
不再可供沙盒 iOS 9 应用程序访问。
From WWDC 2015 session 703 Privacy and Your App:
来自 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
在 iOS 9 中,沙箱现在阻止进程访问其他进程的 kern.proc、kern.procargs 和 kern.procargs2 值
and
和
iOS apps are not permitted to see what other apps are running
不允许 iOS 应用查看其他应用正在运行的内容
So even if you find a way, you are likely to get rejected from the App Store.
因此,即使您找到了方法,也很可能会被 App Store 拒绝。