Detect which app is currently running on iOS using sysctl

时光总嘲笑我的痴心妄想 提交于 2019-11-28 05:32:07
Peteee24

Answering the "currently running" part of your question:

I used the code from this answer Can we retrieve the applications currently running in iPhone and iPad

Looked after the k_proc declarations here: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/sys/proc.h

With trial and error found out that the processes with the p_flag set to 18432 is the currently running application (in this case this test).

See the first link, and replace the inner of the for cycle with:

if (process[i].kp_proc.p_flag == 18432){

                        NSString * processID    = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
                        NSString * processName  = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
                        NSString * status       = [[NSString alloc] initWithFormat:@"%d",process[i].kp_proc.p_flag ];

                        NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName,status, nil]
                                                                            forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName",@"flag", nil]];

                        [array addObject:dict];

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!