如何通过应用程序ID确定哪些应用程序是后台应用程序,哪个应用程序是iOS前台应用程序

使用此问题中描述的方法,我可以获得在iOS设备上运行的应用程序的列表。我知道PID并可以访问其kinfo_proc结构。我如何确定哪些是前台进程,哪些是后台进程(假设我的应用程序是后台进程)?


我试图通过中的信息kinfo_proc(请参阅第一个链接)找到此信息kp_proc.p_priority,但似乎无法根据优先级推断背景/前景状态。


我不太在乎是否可以在AppStore Review中正常工作,但我更喜欢一种无需越狱即可工作的方法(即,私有API可以,但是哪些API?)。我希望它至少在iOS 5上能正常工作


我考虑过编写一个简单的MobileSubstrate扩展程序,将其注入所有应用程序中,然后就钩住每个人的了applicationDidBecomeActive,但这需要越狱,而且过于侵入性。


12345678_0001
浏览 690回答 3
3回答

qq_遁去的一_1

好答案!但是您的代码中有一个小的错字,应该是:首先,请确保已定义SBSERVPATH并包含正确的头文件:#import <sys/sysctl.h>#import <dlfcn.h>#define SBSERVPATH "/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices"然后,首先找到正确的SB端口:mach_port_t *port;void *lib = dlopen(SBSERVPATH, RTLD_LAZY);int (*SBSSpringBoardServerPort)() =&nbsp;dlsym(lib, "SBSSpringBoardServerPort");port = (mach_port_t *)SBSSpringBoardServerPort();&nbsp;dlclose(lib);然后找到活动的应用程序:mach_port_t * port = [self getSpringBoardPort];// open springboard libvoid *lib = dlopen(SBSERVPATH, RTLD_LAZY);// retrieve function SBFrontmostApplicationDisplayIdentifiervoid *(*SBFrontmostApplicationDisplayIdentifier)(mach_port_t *port, char *result) =dlsym(lib, "SBFrontmostApplicationDisplayIdentifier");// reserve memory for namechar appId[256];memset(appId, 0, sizeof(appId));// retrieve front app nameSBFrontmostApplicationDisplayIdentifier(port, appId);// close dynlibdlclose(lib);

慕虎7371278

这对所有IOS设备都适用:#define UIKITPATH "/System/Library/Framework/UIKit.framework/UIKit"#define SBSERVPATH "/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices"&nbsp;- (NSArray*) getActiveApps&nbsp;{mach_port_t *p;void *uikit = dlopen(UIKITPATH, RTLD_LAZY);int (*SBSSpringBoardServerPort)() =dlsym(uikit, "SBSSpringBoardServerPort");p = (mach_port_t *)SBSSpringBoardServerPort();dlclose(uikit);if(self.frameWorkPath == nil || self.frameWorkPath.length == 0){&nbsp; &nbsp; self.frameWorkPath = @SBSERVPATH;&nbsp; &nbsp; self.frameWorkPath = [self.frameWorkPath stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];}const char *cString = [self.frameWorkPath cStringUsingEncoding:NSUTF8StringEncoding];//const char *bar = [self.frameWorkPath UTF8String];void *sbserv = dlopen(cString, RTLD_LAZY);NSArray* (*SBSCopyApplicationDisplayIdentifiers)(mach_port_t* port, BOOL runningApps,BOOL debuggable) =dlsym(sbserv, "SBSCopyApplicationDisplayIdentifiers");//SBDisplayIdentifierForPID - protype assumed,verification of params donevoid* (*SBDisplayIdentifierForPID)(mach_port_t* port, int pid,char * result) =dlsym(sbserv, "SBDisplayIdentifierForPID");//SBFrontmostApplicationDisplayIdentifier - prototype assumed,verification of params done,don't call this TOO often(every second on iPod touch 4G is 'too often,every 5 seconds is not)void* (*SBFrontmostApplicationDisplayIdentifier)(mach_port_t* port,char * result) =dlsym(sbserv, "SBFrontmostApplicationDisplayIdentifier");//Get frontmost applicationchar frontmostAppS[512];memset(frontmostAppS,sizeof(frontmostAppS),0);SBFrontmostApplicationDisplayIdentifier(p,frontmostAppS);NSString * frontmostApp=[NSString stringWithFormat:@"%s",frontmostAppS];if([self iOsMajorVersion] >= 7){&nbsp; &nbsp; NSNumber *topmost = [NSNumber numberWithBool:YES];&nbsp; &nbsp; NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];&nbsp; &nbsp; NSMutableArray&nbsp; * splitted = [frontmostApp componentsSeparatedByString:@"."];&nbsp; &nbsp; if(frontmostApp.length > 0 && splitted != nil && splitted.count > 1 && topmost.boolValue == YES){&nbsp; &nbsp; &nbsp; &nbsp; NSString *appname = [splitted lastObject];&nbsp; &nbsp; &nbsp; &nbsp; [dict setObject:[appname capitalizedString] forKey:@"ProcessName"];&nbsp; &nbsp; &nbsp; &nbsp; [dict setObject:frontmostApp forKey:@"ProcessID"];&nbsp; &nbsp; &nbsp; &nbsp; [dict setObject:frontmostApp forKey:@"AppID"];&nbsp; &nbsp; &nbsp; &nbsp; [dict setObject:topmost forKey:@"isFrontmost"];&nbsp; &nbsp; &nbsp; &nbsp; NSLog(@"Running TOPMOST App %@",dict);&nbsp; &nbsp; &nbsp; &nbsp; return @[dict];&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; return nil;&nbsp; &nbsp; }}//NSLog(@"Frontmost app is %@",frontmostApp);//get list of running apps from SpringBoardNSArray *allApplications = SBSCopyApplicationDisplayIdentifiers(p,NO, NO);//Really returns ACTIVE applications(from multitasking bar)&nbsp; &nbsp;NSLog(@"Active applications:");&nbsp;for(NSString *identifier in allApplications) {&nbsp; &nbsp; &nbsp;// NSString * locName=SBSCopyLocalizedApplicationNameForDisplayIdentifier(p,identifier);&nbsp; &nbsp; &nbsp;NSLog(@"Active Application:%@",identifier);&nbsp;}//get list of all apps from kernelint 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 {&nbsp; &nbsp; size += size / 10;&nbsp; &nbsp; newprocess = realloc(process, size);&nbsp; &nbsp; if (!newprocess){&nbsp; &nbsp; &nbsp; &nbsp; if (process){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free(process);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return nil;&nbsp; &nbsp; }&nbsp; &nbsp; process = newprocess;&nbsp; &nbsp; st = sysctl(mib, miblen, process, &size, NULL, 0);} while (st == -1 && errno == ENOMEM);if (st == 0){&nbsp; &nbsp; if (size % sizeof(struct kinfo_proc) == 0){&nbsp; &nbsp; &nbsp; &nbsp; int nprocess = size / sizeof(struct kinfo_proc);&nbsp; &nbsp; &nbsp; &nbsp; if (nprocess){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSMutableArray * array = [[NSMutableArray alloc] init];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = nprocess - 1; i >= 0; i--){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int ruid=process[i].kp_eproc.e_pcred.p_ruid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int uid=process[i].kp_eproc.e_ucred.cr_uid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //short int nice=process[i].kp_proc.p_nice;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //short int u_prio=process[i].kp_proc.p_usrpri;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; short int prio=process[i].kp_proc.p_priority;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BOOL systemProcess=YES;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ruid==501){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; systemProcess=NO;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char * appid[256];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memset(appid,sizeof(appid),0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int intID,intID2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intID=process[i].kp_proc.p_pid,appid;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SBDisplayIdentifierForPID(p,intID,appid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSString * appId=[NSString stringWithFormat:@"%s",appid];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (systemProcess==NO)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ([appId isEqualToString:@""])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //final check.if no appid this is not springboard app&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //NSLog(@"(potentially system)Found process with PID:%@ name %@,isSystem:%d,Priority:%d",processID,processName,systemProcess,prio);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BOOL isFrontmost=NO;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ([frontmostApp isEqualToString:appId])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isFrontmost=YES;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSNumber *isFrontmostN=[NSNumber numberWithBool:isFrontmost];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName,appId,isFrontmostN, nil]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName",@"AppID",@"isFrontmost", nil]];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NSLog(@"PID:%@, name: %@, AppID:%@,isFrontmost:%d",processID,processName,appId,isFrontmost);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [array addObject:dict];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free(process);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return array;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}dlclose(sbserv);&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS