iOS 9 之前,获取正在运行的进程列表的方法是使用sysctl这个方法
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);
process = NULL;
}
return nil;
}
process = newprocess;
st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM);
但iOS9之后,sysctl()方法已经禁止被沙盒中的App调用。
The first technique was to use the sysctl() function to retrieve the process table (a remnant of OS X), which includes the list of running Apps. In iOS 9, sysctl() was modified to no longer allow sandboxed Apps to retrieve information about other running processes.
如果要想解决『在iOS9系统下,一个通过企业签名分发的App中,获取指定bundleId的应用进程信息』这个问题,我想到了能否使用私有API的这个方法。
回为之前没有做过私有API这种开发,只能通过在github的各种查找,最后发现私有库中
SBApplicationController 有一个方法 applicationWithBundleIdentifier ,能返回一个 SBApplication的对象,有pid这个属性。
代码库:http://gitlab.kidcares.cn/heruyi/appruntime/tree/master/AppTime
然后参照上面的一个叫 AppTime的开源代码,测试了调用私有API的方法,但是在ios 8 系统中,直接返回的是乱码,代码如下:
#define SBSERVPATH “/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices”
#define UIKITPATH “/System/Library/Framework/UIKit.framework/UIKit”
#define PREPATH “/System/Library/PrivateFrameworks/Preferences.framework/Preferences”
-
(void) doTest
{
uikit = dlopen(UIKITPATH, RTLD_LAZY);// frontBord = dlopen(FrontBoard_PATH, RTLD_LAZY);
//
// dlclose(frontBord);SBSSpringBoardServerPort =
dlsym(uikit, “SBSSpringBoardServerPort”);
p = (mach_port_t *)SBSSpringBoardServerPort();
dlclose(uikit);
sbserv = dlopen(SBSERVPATH, RTLD_LAZY);SBSCopyApplicationDisplayIdentifiers =dlsym(sbserv, “SBSCopyApplicationDisplayIdentifiers”);
SBDisplayIdentifierForPID =
dlsym(sbserv, “SBDisplayIdentifierForPID”);
SBFrontmostApplicationDisplayIdentifier = dlsym(sbserv, “SBFrontmostApplicationDisplayIdentifier”);
SBSCopyLocalizedApplicationNameForDisplayIdentifier =
dlsym(sbserv, “SBSCopyLocalizedApplicationNameForDisplayIdentifier”);char topapp[256];
memset(topapp,sizeof(topapp),0);
SBFrontmostApplicationDisplayIdentifier(p,topapp);NSString *bundleId=[NSString stringWithFormat:@"%s",topapp];//获取前台运行的app bundle id
返回的是乱码。
}
所以,想求问各位大神,这种私有API的调用是否只能运行在越狱设备中?iOS 9 通过这种私有API获取进程是个方法吗?
谢谢